Last active
August 29, 2015 14:13
-
-
Save acacha/af000dd6cead9c3b1e1d to your computer and use it in GitHub Desktop.
GetAndroidAccounts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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