I am developing an Android application and I need to retrieve the Google account used on the phone. I want to do this for the C2DM, but I don't want to ask the user to enter in his/her Google email account if they are already logged in. Is there any way to do it?
Asked
Active
Viewed 2.6k times
2 Answers
44
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)

thanhbinh84
- 17,876
- 6
- 62
- 69

Teario
- 721
- 6
- 7
-
Anyone aware of similar solutions compatible with Android 1.6? – Luís Oliveira Dec 14 '10 at 21:20
-
Found an answer here: http://stackoverflow.com/questions/3360926/get-main-gmail-account-username-in-android-2-0 – Luís Oliveira Dec 14 '10 at 21:34
-
2A phone can have more then one gmail account registered, you should enable the user to choose one of this – Janusz Sep 14 '11 at 09:11
-
1Just to clarify, is it possible for two or more members of `list` BOTH to have the type "com.google"? If so, how do you distinguish the logged in one? – Andrew Wyld Mar 19 '12 at 15:33
-
1It is possible, and in that case they are both logged in. I have my personal Google account and the Google account that my employer gave me for work on my phone. I get email alerts from both, on the Play store I have sections for both accounts in my 'Installed' list and the Calendar app shows entries from both accounts. In such a situation you'd most likely be after the device's main account (the one entered first, that can't be unlinked without a factory reset). Unfortunately I don't know of a way to tell which one is the main account. – Teario Apr 20 '12 at 22:33
-
does it still work ? I am trying same but not working ? – SimpleCoder Feb 19 '19 at 10:03
1
I have try below scope to get email address and username
Get Google account in your mobile
public String getMailId() {
String strGmail = null;
try {
Account[] accounts = AccountManager.get(this).getAccounts();
Log.e("PIKLOG", "Size: " + accounts.length);
for (Account account : accounts) {
String possibleEmail = account.name;
String type = account.type;
if (type.equals("com.google")) {
strGmail = possibleEmail;
Log.e("PIKLOG", "Emails: " + strGmail);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
strGmail = null;
}
return strGmail;
}
Get Google accounts user name in your mobile
public String getUsername() {
List<String> possibleEmails = null;
try {
AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");
possibleEmails = new LinkedList<>();
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);
}
} catch (Exception e) {
e.printStackTrace();
if (possibleEmails != null) {
possibleEmails.clear();
}
}
if (possibleEmails != null) {
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;
}
} else {
return null;
}
}
declare permissions to your mainfest file.
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

sivaBE35
- 1,876
- 18
- 23