ContactsContract.Groups.CONTENT_URI 를 이용하여 연락처 그룹 정보 가져오기

 

private void getGroup() {

Uri uri = ContactsContract.Groups.CONTENT_URI;

String[] projection = new String[] {
    ContactsContract.Groups._ID,
    ContactsContract.Groups.TITLE,
    ContactsContract.Groups.ACCOUNT_NAME,
    ContactsContract.Groups.ACCOUNT_TYPE,
    ContactsContract.Groups.DELETED,
    ContactsContract.Groups.GROUP_VISIBLE
};
//  String selection = ContactsContract.Groups.DELETED + "=0" + " AND " +
//      ContactsContract.Groups.GROUP_VISIBLE + "=1";

String selection = null;
String sortOrder = null;
Cursor cursor = managedQuery(uri, projection, selection, null, sortOrder);
String groupType = null;
String groupTitle = null;
String accountName = null;
  

while (cursor.moveToNext()) {

Log.d(TAG, "Id ===> " + cursor.getString(0));

Log.d(TAG, "Title ===> " + cursor.getString(1));
Log.d(TAG, "Account Name ===> " + cursor.getString(2));
Log.d(TAG, "Account Type ===> " + cursor.getString(3));
Log.d(TAG, "Deleted ===> " + cursor.getString(4));
Log.d(TAG, "Visible ===> " + cursor.getString(5));
   
groupTitle = cursor.getString(1);
groupType = cursor.getString(3);

getGroupSummaryCount(cursor.getString(0));

}

}

 

ACCOUNT_TYPE 은 그룹이 휴대폰 자체 그룹인지 메일 등 다른 데에서 동기화된 그룹인지를 알려준다.

ACCOUNT_NAME 은 메일 등에서의 그룹이면 메일을 알려준다.

그리고 DELETED 은 그룹이 삭제되었음에도 테이블에서는 사라지지 않아서 조건에 붙여야 했다.  

GROUP_VISIBLE 은 그룹의 visible 상태를 리턴하는데

갤럭시 넥서스에서 외부에서 동기화된 그룹의 visible이 0으로 나왔다. 그래서 조건으로 넣지 않았다.

 

그룹의 멤버수를 알기 위해서 ContactsContract.Groups.CONTENT_SUMMARY_URI 를 이용하면 된다.

 

private void getGroupSummaryCount(String groupId) {

Uri uri = ContactsContract.Groups.CONTENT_SUMMARY_URI;

String[] projection = new String[]  { 

ContactsContract.Groups.SUMMARY_COUNT,
ContactsContract.Groups.ACCOUNT_NAME,
ContactsContract.Groups.ACCOUNT_TYPE

};
String selection = ContactsContract.Groups._ID + "=" + groupId;
Cursor cursor = managedQuery(uri, projection, selection, null, null);
int cnt = 0;
while (cursor.moveToNext()) {

Log.w(TAG, item.getGroupTitle() + ",  SummaryCount ===> " + cursor.getInt(0));

cnt = cursor.getInt(0);

}

}

 

Posted by 애이불비
l