如何从CONTENT URI获取联系人

[英]How to get the contact from the CONTENT URI


I add a custom contact raw to my contacts from my app (like in whatsapp). When I click the custom raw, it open my app. In my viewing activity I check the action and get the content URI from getIntent().getDataString(). It gives me this - content://com.android.contacts/data/10399. Now I want to get the contact details from this URI. But when I check my contact list using the following code.

我从我的应用程序中添加了一个自定义联系人raw(我在whatsapp中)。当我单击自定义原始时,它会打开我的应用程序。在我的查看活动中,我检查操作并从getIntent()。getDataString()获取内容URI。它给了我这个 - 内容://com.android.contacts/data/10399。现在我想从此URI获取联系人详细信息。但是当我使用以下代码检查我的联系人列表时。

void checkContacts(Context context) {
    Cursor cursor = context.getContentResolver().query(
            ContactsContract.Data.CONTENT_URI,
            null,
            null, null, null);
    if (cursor != null) {
        Log.e(TAG, String.valueOf(cursor.getCount()));
        try {
            while (cursor.moveToNext()) {
                Log.e(TAG, cursor.getString(cursor
                        .getColumnIndexOrThrow(ContactsContract.Data.CONTACT_ID)));
                Log.e(TAG, "** " + cursor.getString(cursor
                        .getColumnIndexOrThrow(ContactsContract.Data.RAW_CONTACT_ID)));
            }
        } finally {
            cursor.close();
        }
    }
}

But i couldn't find any CONTACT_ID or RAW_CONTACT_ID related to 10399. So how do I get the contact detail from this URI? I don't want to get all the contacts. I want to get the specific contact related to the URI.

但是我找不到与10399相关的任何CONTACT_ID或RAW_CONTACT_ID。那么如何从这个URI获取联系人详细信息?我不想得到所有联系人。我想获得与URI相关的特定联系人。

1 个解决方案

#1


3  

try something like this

尝试这样的事情

Uri uri = data.getData();
            String[] projection = { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };

            Cursor cursor = getApplicationContext().getContentResolver().query(uri, projection,
                    null, null, null);
            cursor.moveToFirst();

            int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(numberColumnIndex);

            int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            String name = cursor.getString(nameColumnIndex);
            cursor.close();

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/02/23/af5da2d8a2f510657950ac58091cc36f.html



 
  © 2014-2022 ITdaan.com 联系我们: