Skip to content

Instantly share code, notes, and snippets.

@acacha
Last active Aug 29, 2015
Embed
What would you like to do?
GetAndroidAccounts
/* Sergi Tur Badenas
* http://acacha.org/sergitur
* See http://acacha.org/mediawiki/index.php/Android_AccountManager
* for more info
*/
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
/**
* This class uses the AccountManager to get the primary email address of the
* current user.
*/
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment