2017-01-13 14 views
-2

以下は、電話からすべての連絡先を取得するために使用したコードです。ContactsContract.CommonDataKinds.Phoneを使用するとすべての連絡先を取得できません

 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; 
    } 

私は多くのユーザーからのフィードバックを受けていますが、電話機で完全な連絡先を取得できず、ほとんど連絡先を表示できませんでした。

上記のコードに問題はありますか?ありがとう。

答えて

0

このコードを使用し

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デバイスのすべての電話エントリが含まれています。連絡先に電話がない場合は、それに関する情報を取得しません。 連絡先があなたの後にある場合は、ContactsContract.Contacts.CONTENT_URIに問い合わせる必要があります。

連絡先と電話は、Androidでは2つの別々のものです。すべての連絡先に電話番号があるわけではなく、電話番号を個別に照会する必要があります。