2017-01-13 2 views
-2

Unten ist der Code, den ich verwendete, um alle Kontakte vom Telefon zu bekommen.Konnte nicht alle Kontakte erhalten, wenn Sie ContactsContract.CommonDataKinds.Phone verwenden

 public static ArrayList<Recipient> getAllRecipient(Context context) { 

     ArrayList<Recipient> contacts = new ArrayList<>(); 
     Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 

     if (cursor != null) { 
      try { 
       final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 
       final int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
       final int typeIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); 
       final int uriIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI); 

       String displayName, number, uri; 
       while (cursor.moveToNext()) { 

        int type = cursor.getInt(typeIndex); 
        if (type == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) { 
         displayName = cursor.getString(displayNameIndex); 
         number = cursor.getString(numberIndex); 
         number = number.replaceAll("[^0-9+]+", "");//remove all special character and space, just keep digit number and "+" 
         uri = cursor.getString(uriIndex); 
         Recipient recipient = new Recipient(displayName, number, uri); 
         contacts.add(recipient); 
        } 
       } 
      } catch (Exception e) { 
       LogUtil.debug("can't get recipient: " + e.getMessage()); 
      } finally { 
       cursor.close(); 
      } 
     } 
     cursor.close(); 
     return contacts; 
    } 

Ich habe Feedback von vielen Benutzern, sie nicht voll Kontakte in ihren Handys bekommen, zeigen fast Kontakte aber einige Kontakte verpasst.

Gibt es ein Problem mit dem obigen Code? Vielen Dank.

Antwort

0

Mit diesem Code

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
while (phones.moveToNext()) 
{ 


String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 


String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

} 
phones.close(); 
0

Phone.CONTENT_URI umfasst alle Telefoneinträge des Geräts. Wenn ein Kontakt kein Telefon hat, werden Sie keine Informationen darüber erhalten. Wenn Kontakte das sind, wonach Sie suchen, sollten Sie die ContactsContract.Contacts.CONTENT_URI abfragen.

Denken Sie daran, dass Kontakte und Telefon zwei getrennte Dinge für Android sind. Nicht alle Kontakte haben Telefonnummern und Sie müssten die Nummern separat abfragen.

Verwandte Themen