'2017/07/02'에 해당되는 글 1건

728x90

안드로이드 연락처에 그룹명을 생성하고, 그룹에 인원을 추가하는 코드다.


사용법

String group_name ="그룹명";
long groupId = ContactHelper.getGroupId(getContentResolver(), group_name);
if(groupId == 0){ // 해당 그룹이 없으면 추가
    ContactHelper.createNewGroup(getContentResolver(), group_name);

    groupId = ContactHelper.getGroupId(getContentResolver(), group_name);

}
long rawId = ContactHelper.getrawIdFromContactId(getContentResolver(),30255);

// 그룹에 인원 추가
ContactHelper.addContactToGroup(getContentResolver(),rawId,groupId);

※ 이 코드를 계속 실행하면 데이터가 계속 추가되는 현상이 있는데 해결 코드를 추가로 구현해야 할 거 같다.


//****************** Contact Group ***************************/
// 그룹에 인원 추가하기
public static boolean addContactToGroup(ContentResolver contactHelper,long rawId, long groupId) {
    try {
        ContentValues values = new ContentValues();
        values.put(RawContacts.Data.RAW_CONTACT_ID, rawId);
        values.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId);
        values.put(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
        contactHelper.insert(ContactsContract.Data.CONTENT_URI, values);
        return true;
    } catch (Exception e){
        e.printStackTrace();
    }
    return false;
}

// 그룹 ID 조회
public static long getGroupId(ContentResolver contactHelper, String group_name) {
    long group_id = 0;

    Uri contactUri = ContactsContract.Groups.CONTENT_URI;
    String[] projection = {ContactsContract.Groups._ID};
    String where = ContactsContract.Groups.TITLE + " = ?";
    String[] whereParams = {group_name};

    Cursor cursor = contactHelper.query(contactUri, projection, where, whereParams, null);
    if (cursor.moveToFirst()) {
        do {
            group_id = cursor.getLong(0);
            Log.e("title", group_name);
            Log.e("id", Long.toString(group_id));
        } while (cursor.moveToNext());
    }

    return group_id;
}

// 그룹 추가
public static void createNewGroup(ContentResolver contactHelper, String group_name) {
    ContentValues cv = new ContentValues();
    cv.put(ContactsContract.Groups.TITLE, group_name);
    cv.put(ContactsContract.Groups.DELETED, 0);
    cv.put(ContactsContract.Groups.SHOULD_SYNC, true);
    cv.put(ContactsContract.Groups.GROUP_VISIBLE, 1);
    contactHelper.insert(ContactsContract.Groups.CONTENT_URI, cv);
}

// 그룹명 수정
public static void updateGroup(ContentResolver contactHelper, String name, long groupId) {
    ContentValues cv = new ContentValues();
    cv.put(ContactsContract.Groups.TITLE, name);
    contactHelper.update(ContactsContract.Groups.CONTENT_URI, cv,
            ContactsContract.Groups._ID + " = " + groupId, null);
}

// 그룹 삭제
public static void deleteGroup(ContentResolver contactHelper, long groupId) {
    Uri contactUri = ContactsContract.Groups.CONTENT_URI;
    String where = ContactsContract.Groups._ID + " = " + groupId;
    contactHelper.delete(contactUri, where, null);
}

public static long getrawIdFromContactId(ContentResolver contactHelper, long contactId) {
    // ContactsContract.RawContacts._ID = ContactsContract.Data.RAW_CONTACT_ID
    long rawId = -1;
    Uri contactUri = ContactsContract.RawContacts.CONTENT_URI;
    String[] projection = {RawContacts._ID};
    String where = RawContacts.CONTACT_ID + "=?";
    String[] whereParams = new String[]{String.valueOf(contactId)};
    Cursor cursor = null;

    try {
        cursor = contactHelper.query(contactUri, projection, where, whereParams, null);
        if (cursor.moveToFirst()) {
            rawId = cursor.getLong(cursor.getColumnIndex(RawContacts._ID));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return rawId;
}




블로그 이미지

Link2Me

,