Android Code Snippet Get device's Google Account

Discussion in 'Code Snippets' started by hatzisn, Nov 14, 2018.

  1. hatzisn

    hatzisn Active Member Licensed User

    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)


    Code:
    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:

    Code:
    AddPermission("android.permission.GET_ACCOUNTS")
    AddPermission(
    "android.permission.READ_CONTACTS")
    DO NOT FORGET ALSO (AS I DID) TO INCLUDE THESE RUN TIME PERMISSIONS:

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

    Cheers
     
    Last edited: Nov 17, 2018
  2. Filippo

    Filippo Expert Licensed User

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

    hatzisn Active Member Licensed 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?
     
  4. Filippo

    Filippo Expert Licensed User

    Yes, unfortunately I have to confirm it. :(
     
  5. hatzisn

    hatzisn Active Member Licensed 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+ ?
     
  6. Filippo

    Filippo Expert Licensed User

    No error message, only zero account.
     
  7. hatzisn

    hatzisn Active Member Licensed User

    Do you have only one account on your device?
     
  8. hatzisn

    hatzisn Active Member Licensed 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)
     
  9. Filippo

    Filippo Expert Licensed User

    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.
     
  10. hatzisn

    hatzisn Active Member Licensed User

    Has anyone else tried that with the same results guys?
     
  11. Robert Valentino

    Robert Valentino Well-Known Member Licensed User

    I'll try your code later

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

    Code:
    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 likes this.
  12. hatzisn

    hatzisn Active Member Licensed User

    Does your code work in Android 8+ ? Did you try mine in 8+ ? Thanks...
     
  13. Robert Valentino

    Robert Valentino Well-Known Member Licensed 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
     
  14. hatzisn

    hatzisn Active Member Licensed User

    I managed to confuse you and get me confused again... :) Are you referring to your code or mine? :):):):):):):)
     
  15. Robert Valentino

    Robert Valentino Well-Known Member Licensed User

    Tried yours and it works fine. So does mine (mine gives all Emails)
     
  16. Filippo

    Filippo Expert Licensed User

    It works!
    I only had this in the manifest:
    AddPermission("android.permission.GET_ACCOUNTS")
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice