2012-12-31 6 views
1

다음 함수는 입력 매개 변수 (예 : +436641234567 또는 +436641234567)로 전화 번호를 가지며 연락처 데이터베이스에서 두 가지 조회를 수행합니다. 이 번호에 속한 사용자를 식별합니다 (이미 작동 함). 사용자의 ID를 사용하려면 모든 그룹을 가져옵니다.이 연락처는에 할당되며 (작동하지 않습니다). 테스트는 올바른 ID를 다시 제공하지만 그룹은 "null"입니다.Android 4.1 +/ContactContract : 주어진 contactI 그룹 (숫자 조회에서 파생 됨)

public String getNameGroupByNumber(Context context, String number) { 
    String name = "?"; 
    String contactId = "0"; 
    String group = "0"; 
    String test = "0"; 

    // Step 1: LookUp Name to given Number 
    ContentResolver contentResolver = context.getContentResolver(); 

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 
    String[] contactProjection = new String[] {BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME }; 
    Cursor contactLookup = contentResolver.query(uri, contactProjection, null, null, null); 

    try { 
     if (contactLookup != null && contactLookup.getCount() > 0) { 
      contactLookup.moveToNext(); 

      name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); 
      contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));  
     } 
    } finally {   
     if (contactLookup != null) { 
      contactLookup.close(); 
     } 
    } 

    Log.d(TAG, "Name: " +name + " ContactId: " + contactId); // works as expected 

    // Step 2: Lookup group memberships of the contact found in step one 
    Uri groupURI = ContactsContract.Data.CONTENT_URI; 
    String[] groupProjection = new String[]{ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID , ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID}; 
    Cursor groupLookup = contentResolver.query(groupURI, groupProjection, ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID+"="+contactId, null, null); 

    try { 

     if (groupLookup != null && groupLookup.getCount() > 0) { 
      groupLookup.moveToNext(); 

      test = groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID)); 
      group = groupLookup.getString(groupLookup.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID)); 

      Log.d(TAG, "Group found with Id: " + test + " and GroupId: " + group); // test is again the contactID from above but group is null 

     } 
    } finally {   
     if (groupLookup != null) { 
      groupLookup.close(); 
     } 
    } 

    return name; 
} 

답변

3
Cursor groupLookup = getContentResolver().query(
     ContactsContract.Data.CONTENT_URI, 
     new String[]{ 
       ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID , 
       ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID 
     }, 
     ContactsContract.Data.MIMETYPE + "=? AND " + ContactContract.Data.CONTACT_ID + "=?", 
     new String[]{ 
       ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, 
       contact_id 
     }, null 
); 

사용이 groupLookup 커서를 대신.

Cursor groupCursor = getContentResolver().query(
     ContactsContract.Groups.CONTENT_URI, 
     new String[]{ 
       ContactsContract.Groups._ID, 
       ContactsContract.Groups.TITLE 
     }, ContactsContract.Groups._ID + "=" + _id, null, null 
); 

groupCursor은 _id에서 그룹 제목을 얻는 데 도움이됩니다.

+0

고마워 .... (ContactSContract에서 하나의 임무에도 불구하고) 상자에서 작동합니다. 나는 조금 혼란 스럽다. 그룹 ID (예 : 1, 2, 4)가 다음 코드 (가장 낮은 ID가 3 인 그룹)를 질의 할 때 얻을 수없는 것을 반환한다. \t final String [] GROUP_PROJECTION = new String [] {ContactsContract.Groups._ID, ContactsContract.Groups.TITLE}; \t 커서 cursor = contentResolver.query (ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, ContactsContract.Groups.DELETED + "= 0", null, ContactsContract.Groups.TITLE); Android 또는 Google 서클에 별표가있는 항목이 있습니까? 어떤 생각? –

+0

언급 한 것을 잊어 버렸습니다. 삭제를 위해 표시된 그룹을 쿼리 할 때 해당 ID가 표시되지 않습니다. –

+1

답변을 수정했습니다. 그룹 제목을 얻기 위해'groupCursor'를 사용했습니다. 그러나'ContactsContract.Groups.DELETED + "= 1"'이있는 그룹으로 체크하지 않았습니다. 이 프로그램은'ContactsContract.Groups.DELETED + "= 0"'이있는 그룹에서 그룹 제목을 얻는 데 적합합니다. –