1

我正在编写代码以获取所选联系人的电子邮件,但是当我选择联系人并返回主要活动时,我无法收到电子邮件(未显示吐司)...

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();
        }
    }
}
4

1 回答 1

1

您需要执行另一个查询来检索电子邮件地址。
检查这个答案(关于电子邮件地址的底部),这是同一个问题:从 android 联系人选择器获取联系信息

编辑:代码示例

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // ADD IF STATEMENTS HERE TO CHECK the requestCode, resultCode and data

    Uri contactData = data.getData();
    Cursor c = getContentResolver().query(contactData, null, null, null, null);

    // has result
    if (c.moveToFirst()) {
        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

        // new query for the mail
        Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);

        // has result
        if(emailCursor.moveToFirst()) {
            String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            String emailType = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

            Log.d(getClass().getName(), "EMAIL = " + email);
        }

        emailCursor.close();
    }

    c.close();
}

并且不要忘记在清单中添加 READ_CONTACTS 权限。

于 2013-06-03T21:49:04.210 回答