Android Question Corporate and other accounts

Dario126

Member
Licensed User
Longtime User
I'm using following code to get email list of accounts from mobile device

B4X:
Public Sub GetAccounts As List
    Dim r As Reflector
    Dim wAccounts As List
    wAccounts.Initialize
    r.Target = r.RunStaticMethod("android.accounts.AccountManager", "get", Array As Object(r.GetContext), Array As String("android.content.Context"))
    Dim accounts() As Object
    accounts = r.RunMethod2("getAccountsByType", "com.google", "java.lang.String")
    For i = 0 To accounts.Length - 1
          r.Target = accounts(i)
        wAccounts.Add(r.GetField ("name"))
      Next
    Return wAccounts
End Sub

But this thus not return corporate account which user can have (sometimes as only account). What I need to change to list all acounts, not only google?

Tried to remove "com.google", but then I get nothing.
 

Dario126

Member
Licensed User
Longtime User
Ah ok, it should be "getAccounts" then it works.

Only confusion is I get default account (default goolge account in phone) three times. For example I get those account:
- PHONE
- SIM
- FirstGoogleAcoount (default)
- CorporateAccount
- FirstGoogleAcoount (default)
- FirstGoogleAcoount (default)

Why this default account is tripled ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't get multiple accounts when I test it here.

It should be simple to remove duplicates:
B4X:
Sub Activity_Create(FirstTime As Boolean)
  Dim r As Reflector
  Dim wAccounts As Map
  wAccounts.Initialize
  r.Target = r.RunStaticMethod("android.accounts.AccountManager", "get", Array As Object(r.GetContext), Array As String("android.content.Context"))
  Dim accounts() As Object
  accounts = r.RunMethod("getAccounts")
  For i = 0 To accounts.Length - 1
  r.Target = accounts(i)
      wAccounts.Put(r.GetField ("name"), "")
  Next
  For Each account As String In wAccounts.Keys
     Log(account)
   Next
End Sub
 
Upvote 0
Top