Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several ways to do this, shown below.</p> <p><strong>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.</strong></p> <h1>Method A: Use <a href="http://developer.android.com/reference/android/accounts/AccountManager.html" rel="noreferrer">AccountManager</a> (API level 5+)</h1> <p>You can use <code>AccountManager.getAccounts</code> or <code>AccountManager.getAccountsByType</code> to get a list of all account names on the device. Fortunately, for certain account types (including <code>com.google</code>), the account names are email addresses. Example snippet below.</p> <pre><code>Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ Account[] accounts = AccountManager.get(context).getAccounts(); for (Account account : accounts) { if (emailPattern.matcher(account.name).matches()) { String possibleEmail = account.name; ... } } </code></pre> <p>Note that this requires the <code>GET_ACCOUNTS</code> permission:</p> <pre><code>&lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt; </code></pre> <p>More on using <code>AccountManager</code> can be found at the <a href="http://developer.android.com/resources/samples/ContactManager/src/com/example/android/contactmanager/ContactAdder.html" rel="noreferrer">Contact Manager</a> sample code in the SDK.</p> <h1>Method B: Use <a href="http://developer.android.com/reference/android/provider/ContactsContract.Profile.html" rel="noreferrer">ContactsContract.Profile</a> (API level 14+)</h1> <p>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.</p> <p>Below is a full example that uses a <code>CursorLoader</code> to retrieve profile data rows containing email addresses.</p> <pre><code>public class ExampleActivity extends Activity implements LoaderManager.LoaderCallbacks&lt;Cursor&gt; { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getLoaderManager().initLoader(0, null, this); } @Override public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle arguments) { return new CursorLoader(this, // Retrieve data rows for the device user's 'profile' contact. Uri.withAppendedPath( ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY), ProfileQuery.PROJECTION, // Select only email addresses. ContactsContract.Contacts.Data.MIMETYPE + " = ?", new String[]{ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE}, // Show primary email addresses first. Note that there won't be // a primary email address if the user hasn't specified one. ContactsContract.Contacts.Data.IS_PRIMARY + " DESC"); } @Override public void onLoadFinished(Loader&lt;Cursor&gt; cursorLoader, Cursor cursor) { List&lt;String&gt; emails = new ArrayList&lt;String&gt;(); cursor.moveToFirst(); while (!cursor.isAfterLast()) { emails.add(cursor.getString(ProfileQuery.ADDRESS)); // Potentially filter on ProfileQuery.IS_PRIMARY cursor.moveToNext(); } ... } @Override public void onLoaderReset(Loader&lt;Cursor&gt; cursorLoader) { } private interface ProfileQuery { String[] PROJECTION = { ContactsContract.CommonDataKinds.Email.ADDRESS, ContactsContract.CommonDataKinds.Email.IS_PRIMARY, }; int ADDRESS = 0; int IS_PRIMARY = 1; } } </code></pre> <p>This requires both the <code>READ_PROFILE</code> and <code>READ_CONTACTS</code> permissions:</p> <pre><code>&lt;uses-permission android:name="android.permission.READ_PROFILE" /&gt; &lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload