2012-06-05 115 views
3

我將電話的calllog查詢爲ListView。因此,當用戶長時間點擊某個項目時,會出現一個對話框,其中包含「查看聯繫人」選項。爲了能夠查看聯繫人,意圖需要聯繫人ID。 我的問題是,我並不總是看到正確的聯繫。我點擊彼得,彼得的聯繫表出現了。我點擊莎拉,傑森的聯繫表出現。如何通過電話號碼獲取聯繫人ID

我一定是錯誤地使用了這段代碼。請幫忙。

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。

我也試過費利佩的回答here,也是如此。

編輯:

  • 上的HTC Desire HD(2.3.5)獲得正確的接觸中的 病例99%。
  • ZTE Blade(2.2)我在60%的案例中獲得了正確的聯繫方式。
  • 三星Galaxy Ace(2.3.3)我在5%的情況下獲得了正確的聯繫方式。

這到底是怎麼回事?

回答

4

在挖掘了自己的主題之後,我發現了sven在googlegroup上的幫助。問題在於我使用了一種被貶低的意圖。我通過開發人員網站上的許多頁面閱讀,我一定錯過了它。

我也發佈了完整的代碼。

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

爲什麼26?爲什麼是26? –

0

在這裏,在這種情況下,我要告訴你,如何獲得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(); 
相關問題