Use contentprovider to obtain the contact information on the mobile phone

Source: Internet
Author: User

The source code of Google, the contact person in the mobile phone, has been shared by contentprovider. The bottom layer of contentprovider is implemented by SQLite database, so all its operations on data are implemented by SQL, only URI is provided at the upper layer.

You can create contentprovider in two ways:

1. Create your own contentprovider

2. Add your data to an existing contentprovider, provided that you have the same data type and the write permission.

The contentresolver provided by android can be used to access the data provided by contentprovider.

The following shows how to use contentresolver to obtain the numbers of contacts and contacts on the mobile phone:

Public list <friend> loadcontacts () {list <friend> List = new arraylist <friend> (); // obtain the client contentresolver Cr = context. getcontentresolver (); cursor = CR. query (contactscontract. contacts. content_uri, null, null); If (cursor. movetofirst () {do {friend = new friend (); // contact name string contactsname = cursor. getstring (cursor. getcolumnindex (contactscontract. contacts. display_name); // The ID string PID of the contact in the database = cursor. getstring (cursor. getcolumnindex (contactscontract. contacts. _ id); // we recommend that you use lookup_key to replace cursor phonecursor = CR. query (contactscontract. commondatakinds. phone. content_uri, null, contactscontract. commondatakinds. phone. contact_id + "=" + PID, null, null); string number = NULL; If (phonecursor. movetofirst () {do {// Phone Number = phonecursor. getstring (phonecursor. getcolumnindex (contactscontract. commondatakinds. phone. number);} while (phonecursor. movetonext (); friend. name = contactsname; friend. phone = number; List. add (friend) ;}while (cursor. movetonext ();} return list ;}

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.