2017-06-02 1 views
0

私は電話番号を持っています。私は電話の連絡先からその電話番号の詳細を知りたい。その番号には、名前、組織、電話番号(仕事)、電話番号(家)、電子メール(自宅)、電子メール(仕事)、住所が格納されています。アンドロイド携帯電話の連絡先リストから指定した番号の組織の詳細とメールの詳細を取得しますか?

私は、次のコード

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

これは、2段階のプロセスです:

  1. 電話番号から接触-IDを取得します
  2. ゲット接触-IDから要求されたデータタイプ(電子メール、電話など)

注:
*複数の連絡先に同じ電話番号が設定されている可能性があります。この例では返された最初の連絡先IDのみを受け取り、残りは無視します
* ContactsContractまたはContactsContract.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

whoopsを持っています。手順2の新しい選択肢を参照してください。 – marmor

+0

これは完璧に機能しました。 – manjari

関連する問題