Accessing Google Account Id /username via Android

AndroidGoogle Account

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

The first one is this one: https://stackoverflow.com/questions/4038333/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 : https://stackoverflow.com/questions/3260868/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).

Solution 2 - Android

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" />

Solution 3 - Android

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

Solution 4 - Android

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
}

Solution 5 - Android

Used these lines:

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

the length of array accounts is always 0.

Solution 6 - Android

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

Solution 7 - Android

String name = android.os.Build.USER;

if (!TextUtils.isEmpty(name)) {
	nameEdit.setText(name);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKawBoyView Question on Stackoverflow
Solution 1 - AndroidFabricio BuzetoView Answer on Stackoverflow
Solution 2 - AndroidRahul DevganiyaView Answer on Stackoverflow
Solution 3 - Androidshweta jariyaView Answer on Stackoverflow
Solution 4 - AndroidDixit PanchalView Answer on Stackoverflow
Solution 5 - AndroidErik JohanssonView Answer on Stackoverflow
Solution 6 - AndroidBalajiView Answer on Stackoverflow
Solution 7 - AndroidAhmadullah SaikatView Answer on Stackoverflow