【问题标题】:Open Contact information in Contact List using Phone Number of Contact Android Studio使用 Contact Android Studio 的电话号码打开联系人列表中的联系人信息
【发布时间】:2017-07-08 18:21:08
【问题描述】:

我正在制作一个与电话号码有关的小应用程序。

我的问题是我的手机联系人列表中有一个电话号码。 示例:0877777777

我想使用该电话号码打开该联系人信息。

只是为了澄清我所说的联系人列表是指保存在我手机上的联系人。

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: java android android-contacts


    【解决方案1】:

    PhoneLookup api 仅用于此目的。

    String number = "0877777777";
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] projection = new String[]{ PhoneLookup._ID, PhoneLookup.DISPLAY_NAME };
    
    Cursor cur = getContentresolver().query(uri, projection, null, null, null);
    
    if (cur != null) {
       while (cur.moveToNext()) {
          Long id = cur.getLong(0);
          String name = cur.getString(1);
          Log.d("My Contacts", "found: " + id + " - " + name);
       }
       cur.close();
    }
    

    更新

    要在股票联系人应用中打开联系人的个人资料,您需要先获取联系人的CONTACT_ID,然后在Intent 中将其用于外部应用:

    String number = "0877777777";
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] projection = new String[]{ PhoneLookup._ID };
    
    Cursor cur = getContentresolver().query(uri, projection, null, null, null);
    
    // if other contacts have that phone as well, we simply take the first contact found.
    if (cur != null && cur.moveToNext()) {
        Long id = cur.getLong(0);
    
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, String.valueOf(id));
        intent.setData(contactUri);
        context.startActivity(intent);
    
        cur.close();
    }
    

    【讨论】:

    • 感谢您的回答。我的问题是如何将其放入在联系人中显示联系人的意图?
    • 您好,感谢您的更新。我收到一个错误 java.lang.IllegalArgumentException: Invalid column contact_id。你知道这可能来自哪里吗?
    • 我已经在这里回答了你的新问题:stackoverflow.com/q/45043662/819355(顺便说一句,不需要为同一个问题打开多个 Q)
    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 2014-03-30
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多