LOOKUP_KEY和CONTENT_LOOKUP_URI

时间:2012-03-25 16:35:03

标签: android uri

我有以下代码来获取联系人的详细信息。 “数据”:是我在选择联系人后回来的Uri。

我需要确保将来能够找到合适的联系方式,以便我将来可以保存以供将来使用?是“lookupUri”还是“lookupKey”?

    Cursor c =  activity.managedQuery(data, null, null, null, null);
    c.moveToFirst();
    String lookupKey = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY ));
    c.close();

    // Next use that key to access the details of the contact in order to get the name and the photo
    // Also, save it for future use.
    // It will be used when we fetch the details from the database since the photo itself is not saved.
     Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI,lookupKey);

    Uri uri = ContactsContract.Contacts.lookupContact(activity.getContentResolver(), deviceDetails.lookupUri);

2 个答案:

答案 0 :(得分:2)

LookupKey是您要存储的唯一标识符。

FYI; 2.1中有一个错误,当名称发生变化时,未同步的联系人LookupKey会发生变化。

答案 1 :(得分:0)

这很有效。我的例子查找了这个名字;在投影中添加或删除所需的字段。

private String getContactNameFromAndroidKey (String key)
{
  // Run query
  Uri uri = Uri.parse (ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + key);

  String[] projection = new String[] {
    Contacts._ID,
    Contacts.DISPLAY_NAME,
  };

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

  if (!cursor.moveToNext()) // move to first (and only) row.
    throw new IllegalStateException ("contact no longer exists for key");
  String name = cursor.getString(1);
  cursor.close();

  return name;
}