2017-09-17 10 views
1

내가 MIME-TYPE에 의해 휴대 전화에서 연락처를 가져 오기 위해 노력하고, 그래서 난 종류가 연락처를 선택할 수 있습니다선택 연락처

: 여기

ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE 

을 내가 사용하는 방법

public static ArrayList<Contact> fetchContactsFromPhone(@NonNull Context context) { 
    ArrayList<Contact> contacts = new ArrayList<>(); 

    Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; 
    String _ID = ContactsContract.Contacts._ID; 

    String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; 
    String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; 
    Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 
    String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 
    ContentResolver contentResolver = context.getContentResolver(); 
    Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null); 

    if (cursor != null && cursor.getCount() > 0) { 

     while (cursor.moveToNext()) { 

      String contact_id = cursor.getString(cursor.getColumnIndex(_ID)); 

      String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)); 
      long hasPhoneNumber = Long.parseLong(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER))); 

      if (hasPhoneNumber > 0) { 

       Cursor phoneCursor = contentResolver.query(
         PhoneCONTENT_URI, 
         null, 
         Phone_CONTACT_ID + " = " + contact_id + " AND " + ContactsContract.Data.MIMETYPE + " = " + "'"+ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE+"'" 
         , null, null); 


       if (phoneCursor != null) { 
        while (phoneCursor.moveToNext()) { 
         Contact contact = new Contact(); 

         String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); 
         phoneNumber = phoneNumber.replaceAll("[()\\-\\s]", "").trim(); 

         contact.setName(name); 
         contact.setPhoneNum(phoneNumber); 

         contacts.add(contact); 
        } 
        phoneCursor.close(); 
       } 
      } 
     } 
     cursor.close(); 
    } 

    //return data 
    return contacts; 
} 

이 쿼리는 ZERO 연락처를 반환합니다.

어떤 아이디어가 필요한가요?

답변

1

당신은 MIMETYPE = CommonDataKinds.Phone.CONTENT_ITEM_TYPE 데이터 행을 포함하는 테이블 CommonDataKinds.Phone.CONTENT_URI를 조회하고 있습니다. 그러나 MIMETYPE CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE 행을 요구하면 빈 커서가 나타납니다.

기존 코드를 쉽게 수정할 수 있지만 코드가 고정되어 있어도 코드가 매우 느리거나 장치에서 연락처 당 쿼리가 실행될 수도 있습니다. 이는 수 백 또는 수천 개의 쿼리가 될 수 있습니다. 그냥 이름 + 모든 연락처의 번호를해야하는 경우, (모두를 얻기 위해 하나 개의 쿼리) 다음 코드를 시도 :

ArrayList<Contact> contacts = new ArrayList<>(); 

// Make sure you import Phone from ContactsContract.CommonDataKinds.Phone 

String[] projection = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER }; 
Cursor cur = cr.query(Phone.CONTENT_URI, projection, null, null, null); 

while (cur != null && cur.moveToNext()) { 
    long id = cur.getLong(0); 
    String name = cur.getString(1); 
    String number = cur.getString(2); 

    Log.d(TAG, "got " + id + ", " + name + ", " + number); 

    Contact contact = new Contact(); 

    contact.setName(name); 
    contact.setPhoneNum(number); 
    contacts.add(contact); 
} 

if (cur != null) { 
    cur.close(); 
} 

주를 코드와 유사한,이 코드는 각 연락처에 대해 여러 Contact 개체를 만들 수 있음 연락처에 전화가 두 개 이상있는 경우 연락처 당 하나의 Contact 개체 만 원할 경우 전화 목록을 포함하도록 Contact 개체를 수정하고 ArrayList<Contact>HashMap<Long, Contact>으로 변경하여 새 개체를 만드는 대신 기존 개체에 전화를 추가해야합니다.

+0

'제로'결과를 얻는 것과 성능을 향상시키는 문제를 해결할 때 답을 수락합니다. 감사합니다. 그러나 MIME-TYPE으로 필터링하는 조건은 동일한 계정의 다른 연락처를 가져 오는 결과로 다른 계정 [WhatsApp account for ex]에 속하지 않는 연락처를 가져 오는 중입니다. 또한 내 애플 리케이션은 WhatsApp와 같은 연락처를 추가하므로 많은 계정에서 많은 중복 된 연락처를 가져 오는 중입니다! 이걸 극복하는 방법에 대한 아이디어가 있습니까? –

+0

아래 답변 한 코드에서 언급했듯이 하나의 계정 (예 : Google)에서 선택할 때도 하나 이상의 번호가있는 연락처가 중복되므로 계정별로 필터링하면 도움이되지 않습니다. 'ArrayList'를'HashMap'으로 변경해야합니다. 여기서 키는'CONTACT_ID'이고, 동일한 연락처에 속한 여러 숫자를 하나의'Contact' 객체 내에 넣어야합니다. – marmor

+0

예, 맞았습니다!, 고맙습니다. 도움 :) –