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.
-
3Get 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 Answers
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).

- 1
- 1

- 1,243
- 1
- 18
- 29
-
-
@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
-
1A 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
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" />

- 9,064
- 2
- 45
- 59

- 119
- 1
- 5
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();

- 233
- 4
- 7
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
}

- 43,891
- 12
- 98
- 133

- 3,406
- 1
- 11
- 14
-
-
This is based on using the Google+ API which is probably too exotic for most developers – GregD May 24 '16 at 01:11
Used these lines:
AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");
the length of array accounts is always 0.

- 19,402
- 5
- 56
- 72

- 21
- 2
-
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
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
String name = android.os.Build.USER;
if (!TextUtils.isEmpty(name)) {
nameEdit.setText(name);
}

- 11,584
- 9
- 62
- 84
-
2Well, I tried this and it returned me "dpi". Clearly not the user's name. – Nandan Sawant Aug 02 '15 at 08:27