【问题标题】:Get CONTACT_ID for a given number获取给定号码的 CONTACT_ID
【发布时间】:2013-02-27 16:03:34
【问题描述】:

我正在尝试创建一个将String 作为参数的函数,它将返回Contact_ID 用于具有类似String 提供的电话号码的联系人。

我已经找到了下面的代码

private String getContactName (String number) 
{
    String contactName = "";
    ContentResolver context = getContentResolver();

    /// number is the phone number
    Uri lookupUri = Uri.withAppendedPath(
    PhoneLookup.CONTENT_FILTER_URI, 
    Uri.encode(number));

    String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };

    Cursor cur = context.query(lookupUri,mPhoneNumberProjection, null, null, null);
    try 
    {
       if (cur.moveToFirst()) 
       {
          contactName = cur.getString(2);
          return contactName;
       }
    } 
    finally 
    {
        if (cur != null)
            cur.close();
    }
    return contactName;
}

它返回给定号码的所有联系人姓名。 我如何从这里获取联系人 ID?

谢谢!

【问题讨论】:

标签: android


【解决方案1】:

您只需将cur.getString(2) 替换为cur.getString(0)。您已经将_ID 作为投影的一部分(顺便说一句,可以将其简化为_ID)。

【讨论】:

  • 我应该提一下,_ID 只是行 ID 不是 CONTACT_ID
猜你喜欢
  • 2016-08-16
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多