tags:

views:

2161

answers:

2

How do you get the Android's primary e-mail address (or a list of e-mail addresses)?

It's my understanding that on OS 2.0+ there's support for multiple e-mail addresses, but below 2.0 you can only have one e-mail address per device.

+5  A: 

EDIT: You can also use getAccountsByType with com.google as the account type to see if the user has any Google accounts. Also, consider using getSystemService(ACCOUNT_SERVICE) to get a handle to the AccountManager.


In Android 2.0+, accounts (accessible via AccountManager) aren't necessarily associated with an email address. Technically, the user could be using account provider(s) other than Google Accounts which don't use email addresses.

Having said that, the below should work most of the time (in Android 2.0+):

Account[] accounts = AccountManager.get(this).getAccounts();
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.
  String possibleEmail = account.name;
  ...
}

As per the comment, you'll probably want to perform a regex or account type whitelist check depending on your use case.

More on using AccountManager can be found at the Contact Manager sample code in the SDK.

And as a friendly warning, be careful and up front to the user when dealing with account data. If you misuse a user's email address or other personal information, bad things can happen.

Roman Nurik