Android 如何通过电话号码获取联系人id

Android 如何通过电话号码获取联系人id,android,contacts,Android,Contacts,我在列表视图中查询手机的通话记录。因此,当用户长时间单击某个项目时,会出现一个带有选项的对话框,包括“查看联系人”。为了能够查看联系人,意图需要联系人id。 我的问题是,我并不总是能看到正确的联系人。我点击Peter,Peter的联系表出现了。我点击莎拉,杰森的联系表出现了 我一定是用错了这个密码。请帮忙 ContentResolver contentResolver = getContentResolver(); Uri uri = Uri.withAppendedPath(Contacts

我在列表视图中查询手机的通话记录。因此,当用户长时间单击某个项目时,会出现一个带有选项的对话框,包括“查看联系人”。为了能够查看联系人,意图需要联系人id。 我的问题是,我并不总是能看到正确的联系人。我点击Peter,Peter的联系表出现了。我点击莎拉,杰森的联系表出现了

我一定是用错了这个密码。请帮忙

ContentResolver contentResolver = getContentResolver();

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));

Cursor cursor =  contentResolver.query(uri, new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);

if(cursor!=null) {
      while(cursor.moveToNext())
      {
        String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
        contactid2 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
        }
        cursor.close();
}

Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid2));
 startActivity(intent_contacts);
也许我需要的不是电话号码,而是其他号码

我也试过菲利佩的答案,同样的情况也发生了

编辑:

  • 在HTC Desire HD(2.3.5)上,我在99%的屏幕上都能找到合适的联系人 案例
  • 在中兴通讯刀片(2.2)上,我在60%的情况下获得了正确的触点
  • 在三星Galaxy Ace(2.3.3)上,我在5%的情况下获得了正确的联系人

到底发生了什么事?

在深入了解主题后,我发现斯文在谷歌集团上提供了帮助。问题是我使用的是一种非种族主义的意图。我在开发者网站上读了很多页,我一定是不知怎么错过了

我还发布了完整的代码

contactid26 = null;

ContentResolver contentResolver = getContentResolver();

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));

Cursor cursor =  
contentResolver.query(
    uri, 
    new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
    null, 
    null, 
    null);

    if(cursor!=null) {
        while(cursor.moveToNext()){
            String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
            contactid26 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));

        }
        cursor.close();
    }
    if (contactid26 == null) {
        Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
    }
    else {
        Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactid26)));
        //Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid26));
        startActivity(intent_contacts);
    }

在本例中,我将向您展示如何获取ID,以及 输入并要搜索的任何联系人号码的名称 例如,如果输入“否”并希望搜索其名称和id,则使用 如果您希望在中进行进一步修改,此代码将100%正常工作 这个,那么你可以告诉我

         Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phnno.getText().toString()));//phone no 
         String[] proj = new String[] 
           {///as we need only this colum from a table...
             Contacts.DISPLAY_NAME,
             Contacts._ID,
            };
         String id=null;
         String sortOrder1 = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
         Cursor crsr = getContentResolver().query(lookupUri,proj, null, null, sortOrder1);
     while(crsr.moveToNext())
     {
         String name=crsr.getString(crsr.getColumnIndex(Contacts.DISPLAY_NAME));
          id = crsr.getString(crsr.getColumnIndex(Contacts._ID));
          Toast.makeText(this,"Name of this cntct is   : " +name +"\n id is : "+id ,Toast.LENGTH_SHORT).show();
     }
    crsr.close();

为什么是26岁?为什么是26岁?