【问题标题】:How to get contact id from uri?如何从uri获取联系人ID?
【发布时间】:2015-11-02 18:40:39
【问题描述】:

我想获取特定的联系人 ID 和姓名。获得它的最佳方法是什么?

if (requestCode == mActions.REQUEST_PICK_CONTACT && resultCode == RESULT_OK) {
   Uri uriContact = (Uri)data.getData();
        String/int id = ???
        String name = ???
        String phoneNumber = ???
    }
  }

我只想获取我选择数据的联系人,而不是所有联系人。我该怎么做?

【问题讨论】:

标签: android android-intent android-contacts


【解决方案1】:

如果您有联系人 URI,这就是您获取联系人元数据的方式。

String id, name, phone, hasPhone;
int idx;
Cursor cursor = getContentResolver().query(contactUri, null, null, null,   null);
if (cursor.moveToFirst()) {
   idx = cursor.getColumnIndex(ContactsContract.Contacts._ID);
   id = cursor.getString(idx);

   idx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
   name = cursor.getString(idx);

   idx = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
   hasPhone = cursor.getString(idx);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    相关资源
    最近更新 更多