2017-10-11 4 views
2

Ich habe Kontakt zu meinem Adressbuch hinzugefügt, die mehrere Nummer wie unten hat. bekommen alle Handynummer mit einem Kontakt Android

Ich möchte alle 3 Anzahl von "Benutzer" mit ContactsContract Inhalt URI abrufen. Durch die Verwendung von unten Code bekam ich nur einen Kontakt.

Cursor cursorAddressBook = mContentResolver.query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 

    if (cursorAddressBook != null) { 
     while (cursorAddressBook.moveToNext()) { 

      String dataName = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
      String dataNumber = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      int dataType = cursorAddressBook.getInt(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA2)); 
      String contactId = cursorAddressBook.getString(cursorAddressBook.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); 

      Log.e("last updat Name last", dataName); 
      Log.e("last updated No last", dataNumber); 
      Log.e("last updated Type last", dataType); 


     } 
     cursorAddressBook.close(); 
    } 

Antwort

1

Von diesem blogpost alle Telefonnummern eines Kontaktes zu holen Wenn ein Kontakt mehrere Telefonnummern hat, dann können Sie alle Telefonnummern und andere Informationen abrufen Androids mit eingebaute Klassen (Cursor und ContactsContract) in Android. Und Sie müssen die Kontaktnummer basierend auf dem Telefontyp wie (TYPE_MOBILE, TYPE_HOME ect)

{ 
    Cursor cursor = cntx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts list 
    if (contactsCount > 0) 
    { 
     while(cursor.moveToNext()) 
     { 
      String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
      { 
       //the below cursor will give you details for multiple contacts 
       Cursor pCursor = cntx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                   ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                   new String[]{id}, null); 
       // continue till this cursor reaches to all phone numbers which are associated with a contact in the contact list 
       while (pCursor.moveToNext()) 
       { 
         int phoneType   = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 
         //String isStarred  = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED)); 
         String phoneNo = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         //you will get all phone numbers according to it's type as below switch case. 
         //Logs.e will print the phone number along with the name in DDMS. you can use these details where ever you want. 
         switch (phoneType) 
         { 
          case Phone.TYPE_MOBILE: 
           Log.e(contactName + ": TYPE_MOBILE", " " + phoneNo); 
           break; 
          case Phone.TYPE_HOME: 
           Log.e(contactName + ": TYPE_HOME", " " + phoneNo); 
           break; 
          case Phone.TYPE_WORK: 
           Log.e(contactName + ": TYPE_WORK", " " + phoneNo); 
           break; 
          case Phone.TYPE_WORK_MOBILE: 
           Log.e(contactName + ": TYPE_WORK_MOBILE", " " + phoneNo); 
           break;    
          case Phone.TYPE_OTHER: 
           Log.e(contactName + ": TYPE_OTHER", " " + phoneNo); 
           break; 
          default: 
           break; 
         } 
       } 
       pCursor.close(); 
      } 
     } 
     cursor.close(); 
    } 
} 
abrufen
0

hier ist der Code für

String id, name; 
ContentResolver cr = getContentResolver(); 
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder); 

if (cur.getCount() > 0) { 

    while (cur.moveToNext()) { 
     id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
     name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     Log.i(tag, "Id is " + id + "\t Name is" + name); 

     if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 

      Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 

       null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { 
        id 
       }, null); 

      // this second loop will retrieve all the contact numbers for a paricular contact id 
      while (pCur.moveToNext()) { 
       // Do something with phones 

       int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER); 

       String phn = pCur.getString(phNumber); 

       Log.i("phn number", phn); 
      } 
      pCur.close(); 

     } 

    } 

} 
Verwandte Themen