Android Interview ContentProvider get contacts

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.