Android Question Get email accounts returns empty list

trueboss323

Active Member
Licensed User
Longtime User
Hello,
I made a small program to try and get the Google email addresses logged into the device. I tried this code below:

B4X:
Public  Sub GetEMailList As List
       Dim r            As Reflector
         Dim wAccounts    As Map
      
       Dim EMails       As List
      
       EMails.Initialize
      
      
         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
       Dim SignIn       As String
 
         accounts = r.RunMethod("getAccounts")
          
         For i = 0 To accounts.Length - 1           
             r.Target = accounts(i)
          
           SignIn = r.GetField("type")           
          
           Log("Account:" &i &"  " &accounts(i) &"  SignIn:" &SignIn)
          
             wAccounts.Put(r.GetField("name"), "")
       Next

         For Each account As String In wAccounts.Keys
'#if Debug           
             Log(account)
'#end if
           If  account.Length > 0  Then
               EMails.Add(account)
           End If
           Next
    
       Return EMails
End Sub

But when I call it with Log(GetEmailList) it always returns me an empty list. I checked and made sure I have the Google account added to the device. In the Manifest I also added the permission:
AddPermission(android.permission.GET_ACCOUNTS)
And it still gives me an empty list. I am running this on Android 9.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Starting from Android 8, you can no longer get the list of accounts.

You can let the user pick an account:
B4X:
'this code doesn't require any permission.
Sub Globals
   Private ion As Object
End Sub


Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Dim jo As JavaObject
   jo.InitializeStatic("android.accounts.AccountManager")
   StartActivityForResult(jo.RunMethod("newChooseAccountIntent", Array(Null, Null, Array As String("com.google"), Null, Null, Null, Null)))
   Wait For ion_Event (MethodName As String, Args() As Object)
   If Args(0) = -1 Then 'resultCode = RESULT_OK
       Dim i As Intent = Args(1)
       Log(i.GetExtra("authAccount"))
   End If
End Sub


Sub StartActivityForResult(i As Intent)
   Dim jo As JavaObject = GetBA
   ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
   jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
   Dim jo As JavaObject
   Dim cls As String = Me
   cls = cls.SubString("class ".Length)
   jo.InitializeStatic(cls)
   Return jo.GetField("processBA")
End Sub
Note that newChooseAccountIntent is only available in Android 6+ (SDK version 23).
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
Thank you Erel, I modified the code so it will check the Android version:
B4X:
Sub Button1_Click
   Dim ph As Phone
   If ph.SdkVersion >= 26 Then
      
   Dim jo As JavaObject
   jo.InitializeStatic("android.accounts.AccountManager")
   StartActivityForResult(jo.RunMethod("newChooseAccountIntent", Array(Null, Null, Array As String("com.google"), Null, Null, Null, Null)))
   Wait For ion_Event (MethodName As String, Args() As Object)
   If Args(0) = -1 Then 'resultCode = RESULT_OK
       Dim i As Intent = Args(1)
       Log(i.GetExtra("authAccount"))
   Else
       Log("No account chosen")
   End If
  
   Else
  
       Log(GetEMailList)

   End If
End Sub

Is there a way to remove the Get Accounts permission if the Android version is above sdk 26?
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…