【问题标题】:How to get contact id by phone number如何通过电话号码获取联系人ID
【发布时间】:2012-06-09 13:28:22
【问题描述】:

我将手机的通话记录查询到 ListView 中。因此,当用户长按某个项目时,会出现一个对话框,其中包含“查看联系人”等选项。为了能够查看联系人,意图需要联系人 ID。 我的问题是我并不总是能看到正确的联系人。我点击彼得,然后彼得的联系表出现了。我点击 Sarah,Jason 的联系表出现了。

我一定是错误地使用了这段代码。请帮忙。

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

也许我需要的不是 PhoneLookup._ID,而是其他一些 ID。

我也尝试了 Felipe 的回答 here,同样的情况。

编辑:

  • 在 HTC Desire HD (2.3.5) 上,我在 99% 的 案例。
  • 在 ZTE Blade (2.2) 上,我在 60% 的情况下都能获得正确的接触。
  • 在三星 Galaxy Ace (2.3.3) 上,我在 5% 的情况下获得了正确的联系人。

这到底是怎么回事???

【问题讨论】:

    标签: android contacts


    【解决方案1】:

    在深入研究主题后,我在 googlegroups 上找到了 sven 的帮助。问题是我使用了一个过时的意图。我阅读了开发者网站上的许多页面,我一定是不知何故错过了它。

    我还发布了完整的代码。

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

    【讨论】:

    • 为什么是 26?为什么是 26 岁?
    【解决方案2】:

    在这种情况下,我将向您展示如何获取 ID,以及 您输入并想要搜索的任何联系人的姓名,对于 例如,如果您输入 no 并想搜索其名称和 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-26
      • 2011-07-30
      • 1970-01-01
      • 2014-11-11
      • 2015-06-19
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多