I have to retrieve Google username from the device without using the Google API.. I tried some of the codes on stack overflow, but it did not work.. Please help
Asked
Active
Viewed 97 times
1 Answers
0
Use following code :
public class UserEmailFetcher {
static String getEmail(Context context) {
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager);
if (account == null) {
return null;
} else {
return account.name;
}
}
private static Account getAccount(AccountManager accountManager) {
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
} else {
account = null;
}
return account;
}
}
Add one permission :
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Reference answer : this