【问题标题】:How to retrieve contact number using a projection via an Uri with LOOKUP_KEY?如何通过带有 LOOKUP_KEY 的 Uri 使用投影检索联系人号码?
【发布时间】:2018-06-29 07:32:01
【问题描述】:

我正在尝试使用以下代码获取联系人姓名及其电话号码:

String id = readFile("contactlookupkey");
Uri uri = Uri.parse (ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + id);

  String[] projection = new String[] {
    Contacts._ID,
    Contacts.DISPLAY_NAME,
    ContactsContract.CommonDataKinds.Phone.NUMBER
  };

  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);
  String phone=cursor.getString(2);
  cursor.close();

但由于 ContactsContract.CommonDataKinds.Phone.NUMBER,我在执行查询时遇到了非法参数异常。

问题是我没有看到任何其他方法可以在仍然使用 URI+loopkey 的同时获取电话号码。

如何获取电话号码?

【问题讨论】:

    标签: android uri lookup


    【解决方案1】:

    您使用的 URI 是否正确?

    尝试切换:

    ContactsContract.Contacts.CONTENT_LOOKUP_URI 
    

    ContactsContract.CommonDataKinds.Phone.CONTENT_URI
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-19
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多