Notice
Recent Posts
Recent Comments
Link
관리 메뉴

설.현.아빠

주소록 읽기 본문

교재 & 강좌/안드로이드 프로그래밍 정복

주소록 읽기

설.현.아빠 2011. 5. 31. 11:21



주소록 소스는 많다...


But!!! 내 맘에 꼭드는 소스가 없다...


만들었다...어렵다..ㅋㅋㅋ사진도 추가를 하고 싶지만....차후에^^



 

       private void getContactData() {

             ContentResolver cr = getContentResolver();

             Cursor contacts_cursor = cr.query(

                           ContactsContract.Contacts.CONTENT_URI,

                           new String[]{ContactsContract.Contacts._ID

                                        ContactsContract.Contacts.DISPLAY_NAME},

                           ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1", null,

                           ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

 

             int ID_Index = contacts_cursor.getColumnIndex(ContactsContract.Contacts._ID);

             int NAME_Index = contacts_cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

 

             mLength = contacts_cursor.getCount();

             while (contacts_cursor.moveToNext()) {

                    String id = contacts_cursor.getString(ID_Index);

                    Cursor number_cursor = cr.query(

                                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,

                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",

                                 new String[] { id }, null);

                    int TYPE_Index = number_cursor

                                 .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);

                    int NUM_Index = number_cursor

                                  .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

 

                    while (number_cursor.moveToNext()) {

                           switch (number_cursor.getInt(TYPE_Index)) {

                           case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:

                                 nameList.add(contacts_cursor.getString(NAME_Index));

                                 numberList.add(number_cursor.getString(NUM_Index).replace("-", ""));

                           }

                    }

                    number_cursor.close();

             }

             contacts_cursor.close();

       }




전화번호를 이용해서 이름을 가져오는 예제


 

       public String comparePhoneNumber(ContentResolver cr, String phoneNumber){

             if (L.LOGV) {

                    L.d("C2dm_BroadcastReceiver : comparePhoneNumber = " + phoneNumber);

             }

             Cursor cursor = cr.query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, phoneNumber),

                           new String[] {PhoneLookup.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER}, null, null, null);

            

             while(cursor.moveToNext()){

             if(!cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)).equals("0")){

                           return cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));

                    }

             }

             return null;

       }



http://gnuteam.tistory.com/tag/contactscontract

Comments