How to call Android contacts list?

前端 未结 13 2263
傲寒
傲寒 2020-11-22 02:50

I\'m making an Android app, and need to call the phone\'s contact list. I need to call the contacts list function, pick a contact, then return to my app with the contact\'s

13条回答
  •  没有蜡笔的小新
    2020-11-22 03:34

    I am using this method to read Contacts

    public static List readPhoneContacts(Context context) {
            List contactItems = new ArrayList();
            try {
                Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
                        null, null, "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
    
                /*context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",
                        new String[] { id },
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");*/
    
                Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts list
                if (contactsCount > 0) {
                    while (cursor.moveToNext()) {
    
                        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                            ContactItem contactItem = new ContactItem();
                            contactItem.setContactName(contactName);
                            //the below cursor will give you details for multiple contacts
                            Cursor pCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                                    new String[]{id}, null);
                            // continue till this cursor reaches to all phone numbers which are associated with a contact in the contact list
                            while (pCursor.moveToNext()) {
                                int phoneType = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                                //String isStarred      = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
                                String phoneNo = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                //you will get all phone numbers according to it's type as below switch case.
                                //Logs.e will print the phone number along with the name in DDMS. you can use these details where ever you want.
                                switch (phoneType) {
                                    case Phone.TYPE_MOBILE:
                                        contactItem.setContactNumberMobile(phoneNo);
                                        Log.e(contactName + ": TYPE_MOBILE", " " + phoneNo);
                                        break;
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                        contactItem.setContactNumberMobile(phoneNo);
                                        Log.e(contactName + ": TYPE_HOME", " " + phoneNo);
                                        break;
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                        contactItem.setContactNumberMobile(phoneNo);
                                        Log.e(contactName + ": TYPE_WORK", " " + phoneNo);
                                        break;
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
                                        contactItem.setContactNumberMobile(phoneNo);
                                        Log.e(contactName + ": TYPE_WORK_MOBILE", " " + phoneNo);
                                        break;
                                    case Phone.TYPE_OTHER:
                                        contactItem.setContactNumberMobile(phoneNo);
                                        Log.e(contactName + ": TYPE_OTHER", " " + phoneNo);
                                        break;
                                    default:
                                        break;
                                }
                            }
                            contactItem.setSelectedAddress(getContactPostalAddress(pCursor));
                            pCursor.close();
                            contactItems.add(contactItem);
                        }
    
                    }
                    cursor.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    
    
            return contactItems;
        }//loadContacts
    

提交回复
热议问题