2017-06-02 25 views
0

我有一個電話號碼。我想通過我的手機聯繫人瞭解該電話號碼的詳細信息。對於那個號碼我存儲了姓名,組織,電話號碼(工作),電話號碼(家庭),電子郵件(家庭),電子郵件(工作),地址。從android手機的聯繫人列表中檢索組織詳細信息和電子郵件的詳細信息?

我可以用下面的代碼

Uri lookUpUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 
      Uri.encode(incoming_number)); 
    Cursor cursor = getApplicationContext().getContentResolver().query(lookUpUri,null,null,null,null); 
if(cursor.getCount() > 0){ 
     while(cursor.moveToFirst()){ 
      String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
}} 

獲得該號碼的顯示名稱和ID,但我不知道如何獲取其他細節。

請幫我一把。 謝謝。

回答

2

這是一個兩個步驟:

  1. 從電話號碼獲取非接觸式ID
  2. 獲取請求的數據類型(電子郵件,電話等),從非接觸式ID

注意:
*多個聯繫人可能有相同的電話號碼,在這個例子中,我只是採取第一個contact-id返回,並忽略其餘
*請確保您導入以下所有從ContactsContractContactsContract.CommonDataKinds

// Step 1: 
Uri lookUpUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incoming_number)); 
String[] projection = new String[] { PhoneLookup.CONTACT_ID, PhoneLookup.DISPLAY_NAME } 
Cursor cur = getContentResolver().query(lookUpUri,projection,null,null,null); 
if (cur.moveToFirst()){ 
    long id = cur.getLong(0); 
    String name = cur.getString(1); 
    Log.d(TAG, "found contact: " + id + " - " + name); 

    // Step 2: 
    String selection = Data.CONTACT_ID + "=" + id + " AND " + Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "', '" + Organization.CONTENT_ITEM_TYPE + "')"; 
    String projection = new String[] { Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3 }; 
    Cursor cur2 = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null); 
    while (cur2 != null && cur2.moveToNext()) { 
     String mime = cur2.getString(0); // email/phone/company 
     String data = cur2.getString(1); // the actual info, e.g. +1-212-555-1234 
     int type = cur2.getInt(2); // a numeric value representing type: e.g. home/office/personal 
     String label = cur2.getString(3); // a custom label in case type is "TYPE_CUSTOM" 

     String kind = "unknown"; 
     String labelStr = ""; 

     switch (mime) { 
      case Phone.CONTENT_ITEM_TYPE: 
       kind = "phone"; 
       labelStr = Phone.getTypeLabel(getResources(), type, label); 
       break; 
      case Email.CONTENT_ITEM_TYPE: 
       kind = "email"; 
       labelStr = Email.getTypeLabel(getResources(), type, label); 
       break; 
      case Organization.CONTENT_ITEM_TYPE: 
       kind = "company"; 
       labelStr = Organization.getTypeLabel(getResources(), type, label); 
       break; 
     } 
     Log.d(TAG, "got " + kind + " - " + data + " (" + labelStr + ")"); 
    } 
    if (cur2 != null) { 
     cur2.close(); 
    } 
} else { 
    Log.d(TAG, "contact not found"); 
} 
cur.close(); 
+0

感謝類,但它會顯示所有聯繫人的詳細信息...我想要一個特定數量的細節。爲什麼這將是查詢。我有CONTACT_ID – manjari

+0

哎呦,看到第2步中的新選擇,現在應該工作 – marmor

+0

感謝它的完美運作。 – manjari