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

2

22

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?

Tabulate answered 27/10, 2010 at 22:45 Comment(0)
E
44

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)

Eclampsia answered 28/10, 2010 at 1:26 Comment(6)
Anyone aware of similar solutions compatible with Android 1.6?Rann
Found an answer here: #3361426Rann
A phone can have more then one gmail account registered, you should enable the user to choose one of thisBoydboyden
Just to clarify, is it possible for two or more members of list BOTH to have the type "com.google"? If so, how do you distinguish the logged in one?Chang
It is possible, and in that case they are both logged in. I have my personal Google account and the Google account that my employer gave me for work on my phone. I get email alerts from both, on the Play store I have sections for both accounts in my 'Installed' list and the Calendar app shows entries from both accounts. In such a situation you'd most likely be after the device's main account (the one entered first, that can't be unlinked without a factory reset). Unfortunately I don't know of a way to tell which one is the main account.Eclampsia
does it still work ? I am trying same but not working ?Boneblack
E
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 permissions to your mainfest file.

  <uses-permission android:name="android.permission.GET_ACCOUNTS" />
Ephemeron answered 18/3, 2016 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.