我有以下代碼從聯繫人獲取電子郵件地址它從聯繫人獲取所有電子郵件地址,但我想要特定聯繫人電子郵件地址我提到了一些在stackoverflow中的答案,但不滿意。如何從特定聯繫人獲取電子郵件ID
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(); }
你爲什麼不滿足? –
@PankajKumar它不會像我的第二個代碼一樣給出任何結果。 – Arut
發佈您的完整代碼將有所幫助。 – Aravin