2017-10-15 5 views
0

ContentResolver를 사용하여 계정 유형 (whatsapp, viber 등)을 검색하는 방법은 무엇입니까?
이 안드로이드의 연락처에ContentResolver를 사용하여 계정 유형을 얻으려면 어떻게해야합니까?

ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null); 

    if (cur.getCount() > 0) { 
     while (cur.moveToNext()) { 
      String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
       Log.d("log : ","name : " + name + ", ID : " + id); 

       // get the phone number 
       Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
         new String[]{id}, null); 
       while (pCur.moveToNext()) { 
        String phone = pCur.getString(
          pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        String phone1 = pCur.getString(
          pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 
        Log.d("log : ","phone" + phone + " type : "+phone1); 
       } 
       pCur.close(); 
+0

'phone1'은 무엇을 제공합니까? –

+0

phone1 내게 전화 번호 (직장, 집 등 ..) 유형을 알려주지 만 계정 유형 (whatsapp, viber, ..)을 원합니다. – ABOD

답변

0

데이터는 3 개 주 테이블로 분할되어 내 코드입니다 :

  1. Contacts - 각 항목은 하나의 접촉을 나타내며, 함께 그룹 하나 이상의 RawContacts
  2. RawContacts을 - 각 항목은 SyncAdapter (예 : Whatsapp, Google, Facebook, Viber)에 의해 동기화 된 연락처에 대한 데이터를 나타냅니다.이 항목은 여러 데이터 항목을 그룹화합니다.
  3. Data - 연락처에 대한 실제 데이터는,, 등 각 라인은 모든 Contacts을 가서 당신의 코드에 문제가있는
RawContact 단일

에 속하는 데이터의 한 조각 전화 이메일입니다 연락처 당 전화를 쿼리합니다. 원하는 ACCOUNT_NAMEACCOUNT_TYPE이 포함 된 RawContact 테이블을 쿼리하지 않습니다. 또한 장치의 모든 전화기를 가져 오는 데 수백 개의 쿼리를 작성하는 것은 매우 비효율적이며 대신 단일 쿼리로 수행 할 수 있습니다.

// First create a mapping from RAW_CONTACT_ID to ACCOUNT_TYPE (e.g. Whatsapp) 

Map<Long, String> rawContacts = new HashMap<Long, String>(); 
String[] projection1 = {RawContacts._ID, RawContacts.ACCOUNT_TYPE}; 
Cursor cur1 = cr.query(RawContacts.CONTENT_URI, projection1, null, null, null); 
while (cur1 != null && cur1.moveToNext()) { 
    Long id = cur1.getLong(0); 
    String account = cur1.getString(1); 
    rawContacts.put(id, account); 
} 

// Now query for all the PHONES on the device (no need to query the Contacts table at all) 

String[] projection2 = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, Phone.RAW_CONTACT_ID }; 
Cursor cur2 = cr.query(Phone.CONTENT_URI, projection2, null, null, null); 

while (cur2 != null && cur2.moveToNext()) { 
    long id = cur2.getLong(0); 
    String name = cur2.getString(1); 
    String number = cur2.getString(2); // the actual info, e.g. +1-212-555-1234 
    int type = cur2.getInt(3); // a numeric value representing type: e.g. home/office/personal 
    long raw = cur2.getLong(4); 

    Log.d(TAG, "contact: " + id + ", name: " + name + ", number: " + number + ", type: " + type + ", raw-id: " + raw + ", account-type: " + rawContacts.get(raw)); 
} 

P. 두 번째 을 CONTACT_ID으로 만들어 연락처별로 단일 연락처에 대한 모든 정보를 그룹화 할 수 있습니다.

관련 문제