从 android phone 的联系人列表中检索指定号码的组织详细信息和电子邮件详细信息?

Retrieve the Organisation detail and email detail of a specified number from contact list of an android phone?

我有一个 phone 号码。我想从我的 phone 联系人处了解那个 phone 号码的详细信息。对于那个号码,我存储了姓名、组织、phone 号码(工作)、phone 号码(家庭)、电子邮件(家庭)、电子邮件(工作)、地址。

我可以使用以下代码获取该号码的显示名称和 ID

Uri lookUpUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(incoming_number));
    Cursor cursor = getApplicationContext().getContentResolver().query(lookUpUri,null,null,null,null);
if(cursor.getCount() > 0){
        while(cursor.moveToFirst()){
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}}

但我不知道如何检索其他详细信息。

请帮帮我。 谢谢。

这是一个 2 步过程:

  1. 从 phone 号码中获取联系人 ID
  2. 从联系人 ID 中获取请求的数据类型(电子邮件、phones 等)

备注:
* 多个联系人可能有相同的 phone 号码,在这个例子中,我只取第一个返回的联系人 ID,忽略其余的
* 确保从 ContactsContractContactsContract.CommonDataKinds 导入以下所有 类

// Step 1:
Uri lookUpUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incoming_number));
String[] projection = new String[] { PhoneLookup.CONTACT_ID, PhoneLookup.DISPLAY_NAME }
Cursor cur = getContentResolver().query(lookUpUri,projection,null,null,null);
if (cur.moveToFirst()){
    long id = cur.getLong(0);
    String name = cur.getString(1);
    Log.d(TAG, "found contact: " + id + " - " + name);

    // Step 2:
    String selection = Data.CONTACT_ID + "=" + id + " AND " + Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "', '" + Organization.CONTENT_ITEM_TYPE + "')";
    String projection = new String[] { Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3 };
    Cursor cur2 = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null); 
    while (cur2 != null && cur2.moveToNext()) {
        String mime = cur2.getString(0); // email / phone / company
        String data = cur2.getString(1); // the actual info, e.g. +1-212-555-1234
        int type = cur2.getInt(2); // a numeric value representing type: e.g. home / office / personal
        String label = cur2.getString(3); // a custom label in case type is "TYPE_CUSTOM"

        String kind = "unknown";
        String labelStr = "";

        switch (mime) {
            case Phone.CONTENT_ITEM_TYPE: 
                kind = "phone"; 
                labelStr = Phone.getTypeLabel(getResources(), type, label);
                break;
            case Email.CONTENT_ITEM_TYPE: 
                kind = "email";
                labelStr = Email.getTypeLabel(getResources(), type, label);
                break;
            case Organization.CONTENT_ITEM_TYPE: 
                kind = "company";
                labelStr = Organization.getTypeLabel(getResources(), type, label);
                break;
        }
        Log.d(TAG, "got " + kind + " - " + data + " (" + labelStr + ")");
    }
    if (cur2 != null) {
        cur2.close();
    }
} else {
    Log.d(TAG, "contact not found");
}
cur.close();