How to Get the Google Username on Android

How to get android phone user's google account name?

try this

AccountManager manager = AccountManager.get(context);
Account[] accounts = manager.getAccountsByType("com.google");
List<String> username = new LinkedList<String>();

for (Account account : accounts) {
username.add(account.name);
}

and add permission to android manifest

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

How can I get the google username on Android?

As mentioned in the comments, Roman's answer to How to get the Android device's primary e-mail address solves it.
Here's the code i used that will also strip out the username from the email.

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 > 1)
return parts[0];
}
return null;
}

Getting user's name in Android

As suggested by cricket_007, Google Sign-In API provides the user's display name.

Sample Image

How to get the phone user's name?

You can get users email with GET_ACCOUTNS permission but for getting User Name you need to implement some OAuth to access User's Name and other profile informations.

One way is to user Google Sign-in developers.google.com/identity/sign-in/android/people

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

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)

How can I get Primary Email Account of Android phone?

As far as I read, there is no concept of primary email id in android. and there is no way to get e-mail id associated with play store. so what I did is, I have fetched all gmail ids and took the last one, it is not the main email id, but it should be the first added google account in his device. so in normal use cases user won't play with his first added email id. so we can assume it as primary mail id.



Related Topics



Leave a reply



Submit