How to get contacts from native phonebook in android

后端 未结 2 1756
小蘑菇
小蘑菇 2020-11-27 21:53

I want to display all native contacts in a list and make user to add contacts from the list (Multiple contacts)to my application database.How to dothis can any one give me i

2条回答
  •  温柔的废话
    2020-11-27 22:29

    I used this code on Android 2.1. It pulls down anyone who has a phone number (as defined by the String SELECTION variable) and returns a List of type Person. Person is an object that held the name and phone number of the user. You will have to implement a Person object in order to use this code, but it works perfectly:

    public List getContactList(){
            ArrayList contactList = new ArrayList();
    
            Uri contactUri = ContactsContract.Contacts.CONTENT_URI;
            String[] PROJECTION = new String[] {
                    ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts.HAS_PHONE_NUMBER,
            };
            String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";
            Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION, null, null);
    
    
            if (contacts.getCount() > 0)
            {
                while(contacts.moveToNext()) {
                    Person aContact = new Person();
                    int idFieldColumnIndex = 0;
                    int nameFieldColumnIndex = 0;
                    int numberFieldColumnIndex = 0;
    
                    String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));
    
                    nameFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.DISPLAY_NAME);
                    if (nameFieldColumnIndex > -1)
                    {
                        aContact.setName(contacts.getString(nameFieldColumnIndex));
                    }
    
                    PROJECTION = new String[] {Phone.NUMBER};
                    final Cursor phone = managedQuery(Phone.CONTENT_URI, PROJECTION, Data.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}, null);
                    if(phone.moveToFirst()) {
                        while(!phone.isAfterLast())
                        {
                            numberFieldColumnIndex = phone.getColumnIndex(Phone.NUMBER);
                            if (numberFieldColumnIndex > -1)
                            {
                                aContact.setPhoneNum(phone.getString(numberFieldColumnIndex));
                                phone.moveToNext();
                                TelephonyManager mTelephonyMgr;
                                mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                                if (!mTelephonyMgr.getLine1Number().contains(aContact.getPhoneNum()))
                                {
                                    contactList.add(aContact);  
                                }
                            }
                        }
                    }
                    phone.close();
                }
    
                contacts.close();
            }
    
            return contactList;
        }
    

    EDIT: A rudimentary Person class:

    public class Person {
        String myName = "";
        String myNumber = "";
    
        public String getName() {
            return myName;
        }
    
        public void setName(String name) {
            myName = name;
        }
    
        public String getPhoneNum() {
            return myNumber;
        }
    
        public void setPhoneNum(String number) {
            myNumber = number;
        }
    }
    

提交回复
热议问题