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초 정도 소요된다.
|