1. Use ContentProvider to find contacts
Mainactivity.java
public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Getcontacts ();} private void Getcontacts () {//Get Contentresolver object Contentresolver cr = This.getcontentresolver (); Get the cursor in the beginning of a phone book, mostly the query "contacts" TABLE cursor cursor = cr.query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL, NULL); while (Cursor.movetonext ()) {StringBuilder Sblog = new StringBuilder (); Get the contact name (the name shown), the actual content in contactscontract.contacts int nameindex = Cursor.getcolumnindex ( ContactsContract.Contacts.DISPLAY_NAME); String name = cursor.getstring (Nameindex); Sblog.append ("name=" + name + ";"); Get contact ID String contactId = cursor.getstring (Cursor.getcolumnindex (contactscontract.contacts._id)); Query the corresponding phone number based on the contact ID Cursor phonenumbers = cr.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL, ContactsContract.CommonDataKinds.Phone.COntact_id + "=" + contactId, NULL, NULL); Get the phone number (there may be multiple numbers) while (Phonenumbers.movetonext ()) {String Strphonenumber = phonenumbers.getstring ( Phonenumbers.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)); Sblog.append ("phone=" + Strphonenumber + ";"); } phonenumbers.close (); Query the corresponding Email based on the contact ID Cursor emails = cr.query (ContactsContract.CommonDataKinds.Email.CONTENT_URI, NULL, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, NULL, NULL); Get email (there may be multiple emails) while (Emails.movetonext ()) {String Stremail = emails.getstring (Emails.getcolu Mnindex (ContactsContract.CommonDataKinds.Email.DATA)); Sblog.append ("email=" + Stremail + ";"); } emails.close (); LOG.I ("TAG", sblog.tostring ());} Cursor.close (); }}
Android Interview ContentProvider get contacts