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.
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.
Android locked down
GET_ACCOUNTS
recently so some of the answers did not work for me. I got this working on Android 7.0 with the caveat that your users have to endure a permission dialog.AndroidManifest.xml
MainActivity.java
Sadly accepted answer isn't working.
I'm late, but here's the solution for internal Android Email application unless the content uri is changed by provider:
There are several ways to do this, shown below.
As a friendly warning, be careful and up-front to the user when dealing with account, profile, and contact data. If you misuse a user's email address or other personal information, bad things can happen.
Method A: Use AccountManager (API level 5+)
You can use
AccountManager.getAccounts
orAccountManager.getAccountsByType
to get a list of all account names on the device. Fortunately, for certain account types (includingcom.google
), the account names are email addresses. Example snippet below.Note that this requires the
GET_ACCOUNTS
permission:More on using
AccountManager
can be found at the Contact Manager sample code in the SDK.Method B: Use ContactsContract.Profile (API level 14+)
As of Android 4.0 (Ice Cream Sandwich), you can get the user's email addresses by accessing their profile. Accessing the user profile is a bit heavyweight as it requires two permissions (more on that below), but email addresses are fairly sensitive pieces of data, so this is the price of admission.
Below is a full example that uses a
CursorLoader
to retrieve profile data rows containing email addresses.This requires both the
READ_PROFILE
andREAD_CONTACTS
permissions:This is quite the tricky thing to do in Android and I haven't done it yet. But maybe these links may help you:
This could be useful to others:
Using AccountPicker to get user's email address without any global permissions, and allowing the user to be aware and authorize or cancel the process.
I would use Android's AccountPicker, introduced in ICS.
And then wait for the result: