简体   繁体   English

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

[英]Open Contact information in Contact List using Phone Number of Contact Android Studio

I am making a little app that has to do with phone numbers. 我正在制作一个与电话号码有关的小应用程序。

My question is I have a phone number that is in my Contact List in my phone. 我的问题是我手机中的联系人列表中有一个电话号码。 Example: 0877777777 示例:0877777777

I would like to open that Contacts information using that phone number. 我想使用该电话号码打开该联系人信息。

Just to clarify the when I say contact list I mean the Contacts saved on my Phone. 只是为了澄清当我说联系人列表时,我的意思是联系人保存在我的手机上。

If anyone could help me out I would appreciate it. 如果有人能帮助我,我会很感激。

The PhoneLookup api is meant just for this. 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();
}

UPDATE UPDATE

To open the contact's profile in the stock contacts app, you need to first get the contact's CONTACT_ID , and then use it in an Intent to the external app: 要在股票联系人应用程序中打开联系人的个人资料,您需要首先获取联系人的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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM