78

How do you access the user's Google Account Id / username in code? I am building an application that will call a web service to store data and I want to identify the identity of the person submitting the data.

Wayne Conrad
  • 90,071
  • 22
  • 147
  • 183
KawBoy
  • 931
  • 1
  • 7
  • 9
  • 3
    Get Google Account Username and Email http://stackoverflow.com/a/31538053/4395114 – Mansukh Ahir Jul 21 '15 at 11:51
  • There is also a library for account management in android [here](http://stackoverflow.com/questions/24227274/how-to-add-programmatically-a-custom-account-in-android/39686087#39686087). – Ali Nem Sep 25 '16 at 11:56

7 Answers7

47

I've ran into the same issue and these two links solved for me:

The first one is this one: How do I retrieve the logged in Google account on android phones?

Which presents the code for retrieving the accounts associated with the phone. Basically you will need something like this:

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

And to add the permissions in the AndroidManifest.xml

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

Additionally, if you are using the Emulator the following link will help you to set it up with an account : Android Emulator - Trouble creating user accounts

Basically, it says that you must create an android device based on a API Level and not the SDK Version (like is usually done).

Community
  • 1
  • 1
Fabricio Buzeto
  • 1,158
  • 1
  • 16
  • 27
  • how can I get the account avatar ? Thank you so much` – Urchin Aug 11 '15 at 19:13
  • @Urchin this is a little bit out of the scope of this question, but I believe this other one could help ya. http://stackoverflow.com/questions/17905066/get-user-name-avatar-from-google-account – Fabricio Buzeto Aug 13 '15 at 00:00
  • 1
    A bit late but this may help someone if you trying to get account's avatar (http://stackoverflow.com/questions/9128700/getting-google-profile-picture-url-with-user-id) – Abdelilah El Aissaoui May 28 '16 at 22:58
  • There is also a library for account management in android [here](http://stackoverflow.com/questions/24227274/how-to-add-programmatically-a-custom-account-in-android/39686087#39686087). – Ali Nem Sep 25 '16 at 11:56
10

This Method to get Google Username:

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

    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);
    }

    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;
}

simple this method call ....

And Get Google User in Gmail id::

 accounts = AccountManager.get(this).getAccounts();
    Log.e("", "Size: " + accounts.length);
    for (Account account : accounts) {

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

        if (type.equals("com.google")) {
            strGmail = possibleEmail;

            Log.e("", "Emails: " + strGmail);
            break;
        }
    }

After add permission in manifest;

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

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
rzymek
  • 8,181
  • 2
  • 40
  • 52
Rahul Devganiya
  • 119
  • 1
  • 5
6

Retrieve profile information for a signed-in user Use the GoogleSignInResult.getSignInAccount method to request profile information for the currently signed in user. You can call the getSignInAccount method after the sign-in intent succeeds.

GoogleSignInResult result = 
Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
String personGivenName = acct.getGivenName();
String personFamilyName = acct.getFamilyName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();
shweta jariya
  • 205
  • 4
  • 7
2
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
  Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
  String userid=currentPerson.getId(); //BY THIS CODE YOU CAN GET CURRENT LOGIN USER ID
}
josliber
  • 41,865
  • 12
  • 88
  • 126
Dixit Panchal
  • 3,142
  • 1
  • 9
  • 14
2

Used these lines:

AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");

the length of array accounts is always 0.

Scriptable
  • 17,875
  • 4
  • 50
  • 65
  • Please use android device below Android 6.0 - 6.… (API levels 23-…) , because higher version device required run-time permissions and it will always return length 0. – Mandeep Yadav Feb 27 '19 at 11:54
0

There is a sample from google, which lists the existing google accounts and generates an access token upon selection , you can send that access token to server to retrieve the related details from it to identify the user.

You can also get the email id from access token , for that you need to modify the SCOPE

Please go through My Post

Community
  • 1
  • 1
Balaji
  • 1,609
  • 15
  • 19
-4
String name = android.os.Build.USER;

if (!TextUtils.isEmpty(name)) {
    nameEdit.setText(name);
}
Magnilex
  • 10,219
  • 8
  • 49
  • 72