我有以下代码从联系人那里获取电子邮件地址,它从联系人那里获取所有电子邮件 ID,但我想要特定的联系人电子邮件 ID,我在 stackoverflow 中引用了一些答案,但不满足。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_contact);
update_contacts = (Button) findViewById(R.id.button1);
update_name = (EditText) findViewById(R.id.update_name);
update_phonenumber = (EditText) findViewById(R.id.update_phonenumber);
Bundle extras = getIntent().getExtras();
get_name = extras.getString("contact_name");
update_name.setText(get_name);
ContentResolver cr = getContentResolver();
/* 获取显示名称的联系电话 */
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +" = ?",
new String[]{get_name}, null);
if(pCur.getCount() > 0){
while (pCur.moveToNext()) {
id = pCur.getString(
pCur.getColumnIndex(ContactsContract.Contacts._ID));
String number = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
update_phonenumber.setText(number);
}
}
pCur.close();
/* get the email id for the display name */
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{String.valueOf(id)}, null);
if(emailCur.getCount() > 0){
while (emailCur.moveToNext()) {
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Toast.makeText(getApplicationContext(), ""+email, Toast.LENGTH_LONG).show();
}
}
emailCur.close(); }