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

时间:2017-06-02 07:02:23

标签: android android-contacts

我有一个电话号码。我想通过手机联系人了解该电话号码的详细信息。对于该号码,我存储了姓名,组织,电话号码(工作),电话号码(家庭),电子邮件(家庭),电子邮件(工作),地址。

我可以使用以下代码

获取该号码的显示名称和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));
}}

但我不知道如何检索其他细节。

请帮帮我。 谢谢。

1 个答案:

答案 0 :(得分:2)

这是一个两步过程:

  1. 从电话号码
  2. 获取联系人ID
  3. 从contact-id
  4. 获取请求的数据类型(电子邮件,电话等)

    注意:
    *多个联系人可能拥有相同的电话号码,在这个例子中,我只是将第一个联系人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();