728x90

CONTACT_ID 를 구했다는 가정하에 연락처를 삭제하는 메소드이다.

메소드는 2개 모두 잘 동작함을 확인하고 적어둔다.

// 구한 ID 기준으로 연락처 삭제
public static void deleteContactFromRawContactID(ContentResolver contactHelper, long CONTACT_ID) {
    String where = RawContacts.CONTACT_ID + " = " + String.valueOf(CONTACT_ID);
    contactHelper.delete(RawContacts.CONTENT_URI, where, null);
} 

 // 구한 ID 기준으로 연락처 삭제
public static void deleteContactFromContactId(ContentResolver contactHelper, long ContactId) {
    Uri contactUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = null;
    String where = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
    String[] whereParams =new String[] { String.valueOf(ContactId) };
    String sortOrder = null;

    Cursor cursor = contactHelper.query(contactUri, projection, where, whereParams, sortOrder);
    if(cursor.moveToFirst()){
        try {
            do {
                String lookupKey = cursor .getString(cursor .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                Uri uri = Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                contactHelper.delete(uri, null, null);
            } while (cursor.moveToNext());
        } catch (Exception e){
            e.getStackTrace();
        }
    }
}


전화번호로 CONTACT_ID 를 구하는 메소드

// 전화번호에서 구한 PhoneLookup._ID 가 RawContacts.ContactID 와 동일함
public static long getContactIDFromNumber(ContentResolver contactHelper, String number) {
    long rawContactId = -1;
    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] projection = {PhoneLookup._ID};
    Cursor cursor = null;

    try {
        cursor = contactHelper.query(contactUri, projection, null, null, null);
        if (cursor.moveToFirst()) {
            System.out.println("PhoneLookup._ID from number : " + cursor.getLong(cursor.getColumnIndex(PhoneLookup._ID)));
            rawContactId = cursor.getLong(cursor.getColumnIndex(PhoneLookup._ID));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(cursor != null) {
            cursor.close();
        }
    }
    return rawContactId;
}



블로그 이미지

Link2Me

,