我正在编写代码以获取所选联系人的电子邮件,但是当我选择联系人并返回主要活动时,我无法收到电子邮件(未显示吐司)...
public static final int PICK_CONTACT = 1;
public void onclick(View view) {
Intent intent1 = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent1,PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==PICK_CONTACT) {
try {
if(resultCode==Activity.RESULT_OK) {
Uri uri=data.getData();
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};
Cursor emailCur =
getContentResolver().query(uri, null, null, null,null);
emailCur.moveToFirst();
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Toast.makeText(MainActivity.this,
"The email is" +email, Toast.LENGTH_LONG).show();
emailCur.close();
}
}
catch(Exception e) {
e.getCause();
}
}
}