[ACCEPTED]-How do I retrieve the logged in Google account on android phones?-accountmanager

Accepted answer
Score: 45

Something like this should work:

AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();
String gmail = null;

for(Account account: list)
{
    if(account.type.equalsIgnoreCase("com.google"))
    {
        gmail = account.name;
        break;
    }
}

And you 9 will need the following permission in your 8 manifest:

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>

Remember to 'Requesting Permissions 7 at Run Time' if you support Android 6 and 6 later https://developer.android.com/training/permissions/requesting.html

I wrote this from memory so it may 5 need a little tweaking. Apparently it's 4 possible to register now without an email 3 address, so maybe do some regexing on the 2 data to ensure it's actually an email address 1 (ensure it contains @gmail or @googlemail)

Score: 1

I have try below scope to get email address and username

Get Google account in your mobile

 public String getMailId() {
        String strGmail = null;
        try {
            Account[] accounts = AccountManager.get(this).getAccounts();
            Log.e("PIKLOG", "Size: " + accounts.length);
            for (Account account : accounts) {

                String possibleEmail = account.name;
                String type = account.type;

                if (type.equals("com.google")) {

                    strGmail = possibleEmail;
                    Log.e("PIKLOG", "Emails: " + strGmail);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
             strGmail = null;
        }

        return strGmail;
    }

Get Google accounts user name in your mobile

 public String getUsername() {
    List<String> possibleEmails = null;
    try {
        AccountManager manager = AccountManager.get(this);
        Account[] accounts = manager.getAccountsByType("com.google");
        possibleEmails = new LinkedList<>();

        for (Account account : accounts) {
            // TODO: Check possibleEmail against an email regex or treat
            // account.name as an email address only for certain account.type
            // values.
            possibleEmails.add(account.name);
        }
    } catch (Exception e) {
        e.printStackTrace();
        if (possibleEmails != null) {
            possibleEmails.clear();
        }
    }

    if (possibleEmails != null) {
        if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
            String email = possibleEmails.get(0);
            String[] parts = email.split("@");
            if (parts.length > 0 && parts[0] != null) {
                return parts[0];

            } else {
                return null;
            }
        } else {
            return null;
        }
    } else {
        return null;
    }
}

declare 1 permissions to your mainfest file.

  <uses-permission android:name="android.permission.GET_ACCOUNTS" />

More Related questions