Android Code Snippet Get device's Google Account

Hi all,

I recently needed to check for device's google account and I was not able to get it done with Erel's code. I was not getting any results. So a little search on the Internet and a little mixing of the soup and voila:

(it was created for activities but it may be used without any problems - I suppose - also in services)


B4X:
Sub GetPrimaryAccount As String
    Dim r As Reflector
    Dim sRet As String
    Dim j As JavaObject
 
    j.InitializeContext
    sRet = j.RunMethod("getEmailID", Array As Object(r.GetContext))
 
    Return sRet
End Sub

#If JAVA

import android.content.Context;
import android.accounts.AccountManager;
import android.accounts.Account;


public String getEmailID(Object objcntx) {
    Context context = (Context) objcntx;
    AccountManager accountManager = AccountManager.get(context);
    Account account = getAccount(accountManager);
    if (account == null) {
        return null;
    } else {
        return account.name;
    }
};

public Account getAccount(AccountManager accountManager) {
    Account[] accounts = accountManager.getAccountsByType("com.google");
    Account account;
    if (accounts.length > 0) {
        account = accounts[0];
//      account = accounts[accounts.length-1]; ‘για τον τελευταίο=πρώτος μπήκε
    } else {
        account = null;
    }
    return account;
};

#End If


DO NOT FORGET (AS I DID) TO INCLUDE IN YOUR MANIFEST THE FOLLOWING:

B4X:
AddPermission("android.permission.GET_ACCOUNTS")
AddPermission("android.permission.READ_CONTACTS")

DO NOT FORGET ALSO (AS I DID) TO INCLUDE THESE RUN TIME PERMISSIONS:

B4X:
rp.CheckAndRequest(rp.PERMISSION_GET_ACCOUNTS)
rp.CheckAndRequest(rp.PERMISSION_READ_CONTACTS)


Cheers
 
Last edited:
D

Deleted member 103

Guest
Unfortunately this does not work anymore from Andorid 8+ and "...\android-28\android.jar".
 

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi Filippo, have you tried it in a real device with Android 8+? My device is Android 7 but in B4A the path to android.jar is pointed to "...\android-28\android.jar". I compiled it with it and it works perfect in my Android 7 device... Can you please confirm that you tried it in an Android 8+ device?
 
D

Deleted member 103

Guest
Hi Filippo, have you tried it in a real device with Android 8+? My device is Android 7 but in B4A the path to android.jar is pointed to "...\android-28\android.jar". I compiled it with it and it works perfect in my Android 7 device... Can you please confirm that you tried it in an Android 8+ device?

Yes, unfortunately I have to confirm it. :(
 

hatzisn

Well-Known Member
Licensed User
Longtime User
If you try it in debug mode what is the error you get Filippo? That really interests me...

Has anyone else tried it on Android 8+ ?
 
D

Deleted member 103

Guest
If you try it in debug mode what is the error you get Filippo? That really interests me...
No error message, only zero account.
 

hatzisn

Well-Known Member
Licensed User
Longtime User
Do you have only one account on your device?
 

hatzisn

Well-Known Member
Licensed User
Longtime User
Neaah, I AM TOTALLY STUPID.
I forgot to tell you guys to add manifest code and run time permissions code.

Filippo and everyone else, go to the first post and copy the code to your manifest and to activity create. Try it and let me know please...
(rp = RuntimePermissions object)
 
D

Deleted member 103

Guest
Neaah, I AM TOTALLY STUPID.
I forgot to tell you guys to add manifest code and run time permissions code.

Filippo and everyone else, go to the first post and copy the code to your manifest and to activity create. Try it and let me know please...
(rp = RuntimePermissions object)
You can do whatever you want, it does not work anymore on Android 8+. The only way to get to the Google Account is via Firebase Authentication.
 

hatzisn

Well-Known Member
Licensed User
Longtime User
Has anyone else tried that with the same results guys?
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I'll try your code later

But this is how I have been getting Email addresses and it has been working on everything

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 And IsEmail(account)  Then
               EMails.Add(account)
           End If
           Next
     
       Return EMails
End Sub
 

hatzisn

Well-Known Member
Licensed User
Longtime User
Does your code work in Android 8+ ? Did you try mine in 8+ ? Thanks...
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
The permissions where showing (So I set them manually)

Works FINE tried on Samsung Galaxy Tab A - Android 7.0 and Samsung S7 - Android 8.0
 

hatzisn

Well-Known Member
Licensed User
Longtime User
I managed to confuse you and get me confused again... :) Are you referring to your code or mine? :):):):):):):)
 
Top