Created
April 26, 2013 03:49
-
-
Save twocity/5464935 to your computer and use it in GitHub Desktop.
How to get the Android device's primary e-mail address
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
// copy from | |
// http://stackoverflow.com/a/2175688/1350593 | |
public class ExampleActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> { | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
getLoaderManager().initLoader(0, null, this); | |
} | |
@Override | |
public Loader<Cursor> 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<Cursor> cursorLoader, Cursor cursor) { | |
List<String> emails = new ArrayList<String>(); | |
cursor.moveToFirst(); | |
while (!cursor.isAfterLast()) { | |
emails.add(cursor.getString(ProfileQuery.ADDRESS)); | |
// Potentially filter on ProfileQuery.IS_PRIMARY | |
cursor.moveToNext(); | |
} | |
... | |
} | |
@Override | |
public void onLoaderReset(Loader<Cursor> cursorLoader) { | |
} | |
private interface ProfileQuery { | |
String[] PROJECTION = { | |
ContactsContract.CommonDataKinds.Email.ADDRESS, | |
ContactsContract.CommonDataKinds.Email.IS_PRIMARY, | |
}; | |
int ADDRESS = 0; | |
int IS_PRIMARY = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment