如何从android中的本地电话簿获取联系人

我想显示列表中的所有本机联系人,并使用户从列表(多个联系人)添加联系人到我的应用程序数据库。如何任何人可以给我想法或共享一些代码..在此先感谢..

我在Android 2.1上使用这个代码。 它拉下任何有电话号码的人(由stringselectvariables定义),并返回一个Persontypes的List。 Person是持有用户姓名和电话号码的对象。 你将不得不实现一个Person对象才能使用这个代码,但是它完美的工作:

public List<Person> getContactList(){ ArrayList<Person> contactList = new ArrayList<Person>(); 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; } 

编辑 :一个基本的人类:

 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; } } 

此代码在Android 4.2中完美工作。 而且它的工作速度更快,因为您不需要为每个联系人进行额外的查询

 private static final String CONTACT_ID = ContactsContract.Contacts._ID; private static final String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; private static final String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; private static final String PHONE_NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; private static final String PHONE_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; public static ArrayList<Contact> getAll(Context context) { ContentResolver cr = context.getContentResolver(); Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{PHONE_NUMBER, PHONE_CONTACT_ID}, null, null, null ); if(pCur != null){ if(pCur.getCount() > 0) { HashMap<Integer, ArrayList<String>> phones = new HashMap<>(); while (pCur.moveToNext()) { Integer contactId = pCur.getInt(pCur.getColumnIndex(PHONE_CONTACT_ID)); ArrayList<String> curPhones = new ArrayList<>(); if (phones.containsKey(contactId)) { curPhones = phones.get(contactId); } curPhones.add(pCur.getString(pCur.getColumnIndex(PHONE_CONTACT_ID))); phones.put(contactId, curPhones); } Cursor cur = cr.query( ContactsContract.Contacts.CONTENT_URI, new String[]{CONTACT_ID, DISPLAY_NAME, HAS_PHONE_NUMBER}, HAS_PHONE_NUMBER + " > 0", null, DISPLAY_NAME + " ASC"); if (cur != null) { if (cur.getCount() > 0) { ArrayList<Contact> contacts = new ArrayList<>(); while (cur.moveToNext()) { int id = cur.getInt(cur.getColumnIndex(CONTACT_ID)); if(phones.containsKey(id)) { Contact con = new Contact(); con.setMyId(id); con.setName(cur.getString(cur.getColumnIndex(DISPLAY_NAME))); con.setPhone(TextUtils.join(",", phones.get(id).toArray())); contacts.add(con); } } return contacts; } cur.close(); } } pCur.close(); } return null; } 

Contact与答案中的类Person类似。