从 Android APK 25 中的选定联系人获取电子邮件

Fetch email from the selected Contact in Android APK 25

获取权限并开始选择联系人的代码Activity

Button chooseContactsBtn = (Button) findViewById(R.id.addContactButton);
    chooseContactsBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getPermissionToReadUserContacts();
            Intent contacts = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(contacts, PICK_CONTACTS);
        }
    });

上 Activity 结果

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACTS) {
        if (resultCode == RESULT_OK) {
            ContactDataManager contactsManager = new ContactDataManager(this, data);

            try {

                email = contactsManager.getContactEmail();
                EditText e_mail = (EditText) findViewById(R.id.e_mail);
                e_mail.setText(email);

            } catch (ContactDataManager.ContactQueryException e) {
                //Print Exception
            }
        }
    }
}

联系人管理器中的 getContactEmail 方法 class。

public String getContactEmail() throws ContactQueryException
{
    Cursor cursor = null;
    String email = null;
    try
    {
        cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
                new String[]{intent.getData().getLastPathSegment()},
                null);

        if (cursor.moveToFirst())
        {
            email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        }
        else {
            System.out.println("No Email found. and leaving the method.");
        }


    } catch (Exception e)
    {
        Log.e(LOG_TAG, e.getMessage());
        throw new ContactQueryException(e.getMessage());
    } finally
    {
        if (cursor != null)
            cursor.close();
    }

    return email;
}

这没有 return 任何东西。我有 returning 名称和号码的方法,它们工作正常。 联系人权限为正。

您正在调用 phone 选择器 意图(通过您意图中的 CommonDataKinds.Phone.CONTENT_URI uri)。

您可以执行以下两项操作之一: (a) 将您的选择器意图更改为 电子邮件选择器 ,并在 return 中获取特定电子邮件 (b) 将您的选择器意图更改为 联系人选择器,并通过使用基于CONTACT_ID.

我会选择选项 (a),代码是:

Intent contacts = new Intent(Intent.ACTION_PICK, CommonDataKinds.Email.CONTENT_URI); // Note the Email!
startActivityForResult(contacts, PICK_CONTACTS);

然后:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACTS && resultCode == RESULT_OK) {
        // Get the URI and query the content provider for the EMAIL
        Uri contactUri = data.getData();
        String[] projection = new String[]{CommonDataKinds.Email.ADDRESS};
        Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
        // If the cursor returned is valid, get the email
        if (cursor != null && cursor.moveToFirst()) {
            String email = cursor.getString(0);
        }
    }
}

查看更多信息:https://developer.android.com/guide/components/intents-common.html#Contacts