使用 Phone 联系人数量 Android Studio 在联系人列表中打开联系人信息

Open Contact information in Contact List using Phone Number of Contact Android Studio

我正在制作一个与 phone 个数字有关的小应用程序。

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

我想用 phone 号码打开联系人信息。

澄清一下,我所说的联系人列表是指保存在我 Phone 上的联系人。

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

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();
}