Skip to content

Instantly share code, notes, and snippets.

@eighthave
Created June 13, 2013 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eighthave/5777441 to your computer and use it in GitHub Desktop.
Save eighthave/5777441 to your computer and use it in GitHub Desktop.
I get email from accounts, then use that to look up display name
String email = null;
// get email address from first system account that looks like an email
AccountManager manager = AccountManager.get(this);
for (Account account : manager.getAccounts())
if (account.name.contains("@") && account.name.contains(".")) {
email = account.name;
EditText keyEmail = (EditText) findViewById(R.id.keyEmail);
keyEmail.setText(email);
break;
}
if (email == null)
return;
// use that email to look up the name in Contacts
final String[] projection = {
Contacts.DISPLAY_NAME,
CommonDataKinds.Email.DATA,
};
Cursor cursor = getContentResolver().query(
CommonDataKinds.Email.CONTENT_URI,
projection,
CommonDataKinds.Email.DATA + " = ?",
new String[]{email},
null);
if (cursor != null && cursor.getCount() > 0 && cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
EditText keyName = (EditText) findViewById(R.id.keyName);
keyName.setText(name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment