728x90

안드로이드 연락처(Contacts)에서 ContactId 를 구하는 방법 테스트한 것을 적어둔다.


 Contacts._ID = PhoneLookup._ID = Phone.CONTACT_ID = RawContacts.CONTACT_ID

import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract;

Uri contactUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
// 검색할 칼럼 정하기, null 이면 모든 필드
String[] projection = {Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE};
String where = Phone.DISPLAY_NAME + " like \'%" + display_name + "%\' AND " + Phone.TYPE + "=2";
String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
Cursor cursor = cursor = contactHelper.query(contactUri, projection, where, null, sortOrder);
long rawContactId = cursor.getLong(cursor.getColumnIndex(Phone.CONTACT_ID));

Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// 검색할 칼럼 정하기, null 이면 모든 필드
String[] projection = {PhoneLookup._ID, PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER};
Cursor cursor = contactHelper.query(contactUri, projection, null, null, null);
long rawContactId = cursor.getLong(cursor.getColumnIndex(PhoneLookup._ID));

Uri contactUri = ContactsContract.RawContacts.CONTENT_URI;
String[] projection = {RawContacts.CONTACT_ID};
String where = RawContacts._ID + "=?";
String[] whereParams = new String[]{String.valueOf(_ID)};
Cursor cursor = cursor = contactHelper.query(contactUri, projection, where, whereParams, null);
long rawContactId = cursor.getLong(cursor.getColumnIndex(RawContacts.CONTACT_ID));

ContentResolver cr = getContentResolver();
Uri contactUri = Contacts.CONTENT_URI;
String[] projection = new String[] {
        Contacts._ID,
        Contacts.DISPLAY_NAME,
        Contacts.STARRED,
        Contacts.TIMES_CONTACTED,
        Contacts.CONTACT_PRESENCE,
        Contacts.PHOTO_ID,
        Contacts.LOOKUP_KEY,
        Contacts.HAS_PHONE_NUMBER,
};
String where = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + " == 1))";
String sortOrder = Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";;
Cursor cursor = cr.query(contactUri, projection, where, null, sortOrder);
if (cursor.getCount() > 0) {
    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor.getColumnIndex(Contacts._ID));
        String name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
        System.out.println("contactId : " + contactId + " name : " + name);
        String mobileNO = null;
        String officeNO = null;
        if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(Contacts.HAS_PHONE_NUMBER))) > 0) {
            Uri phonetUri = Phone.CONTENT_URI;
            Cursor pCur = cr.query(phonetUri, null, Phone.CONTACT_ID + " = ?", new String[]{contactId}, null);
            while (pCur.moveToNext()) {
                int phoneType = pCur.getInt(pCur.getColumnIndex(Phone.TYPE));
                String phoneNumber = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
                switch (phoneType) {
                    case Phone.TYPE_MOBILE:
                        mobileNO = phoneNumber;
                        break;
                    case Phone.TYPE_HOME:
                        break;
                    case Phone.TYPE_WORK:
                        officeNO = phoneNumber;
                        break;
                    case Phone.TYPE_OTHER:
                        break;
                    default:
                        break;
                }
            }
            pCur.close();
        }
    }
}

Contacts._ID 를 기준으로 구할 경우에는 휴대폰 번호가 바로 나오지 않기 때문에 또한번 Cursor를 사용하기 때문에 속도가 매우 늦다. 2,000개 정도 되는 연락처를 가져오는데 30초 정도 소요된다.


 ContactsContract.RawContacts 의 _ID 로 RawContacts.CONTACT_ID 구하는 코드

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

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



구한 ID 기준으로 연락처 삭제

// 구한 CONTACT_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();
        }
    }
}



블로그 이미지

Link2Me

,