如何從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 联系我们: