Created
June 13, 2013 21:14
-
-
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
This file contains hidden or 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
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