How do I retrieve the logged in Google account on android phones?

I am developing an Android application and I need to retrieve the Google account used on the phone. I want to do this for the C2DM, but I don't want to ask the user to enter in his/her Google email account if they are already logged in. Is there any way to do it?


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 will need the following permission in your manifest:

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

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

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


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 permissions to your mainfest file.

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