使用联系人 Android Studio 的电话号码在联系人列表中打开联系人信息



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

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

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

只是为了澄清当我说联系人列表时,我的意思是保存在手机上的联系人。

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

PhoneLookupapi就是为了这个。

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

最新更新