【问题标题】:Retrieve the Organisation detail and email detail of a specified number from contact list of an android phone?从 Android 手机的联系人列表中检索指定号码的组织详细信息和电子邮件详细信息?
【发布时间】:2017-06-02 07:02:23
【问题描述】:

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

我可以使用以下代码获取该号码的显示名称和 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));
}}

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

请帮帮我。 谢谢。

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    这是一个两步过程:

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

    注意事项:
    * 多个联系人可能有相同的电话号码,在本例中,我将只取第一个返回的联系人 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();
    

    【讨论】:

    • 谢谢,但它正在显示所有联系人的详细信息...我想要特定号码的详细信息。对于那将是什么查询。我有 CONTACT_ID
    • 哎呀,请参阅第 2 步中的新选择,现在应该可以使用了
    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    相关资源
    最近更新 更多