Android uses contectresolver to operate data in other applications

Source: Internet
Author: User

When an application needs to interact with another application, you need to use the two classes provided by Android, contentprovider and contentresolver to implement relevant functions. Contentprovider is used to provide content to other applications for operations. contentresolver is used to operate (including query and insert) other application data. Of course, it can also be used in your own applications. This section describes the usage of contentresolver.

You can use context to obtain the contentresolver instance. In the activity, you can directly use the following sentence:

ContentResolver resolver = this.getContentResolver();  

After obtaining this object, you can implement the following common functions:

// Query: Public final cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder); // Add public final URI insert (uri url, contentvalues values) // update public final int Update (URI Uri, contentvalues values, string where, string [] selectionargs) // Delete public final int Delete (uri url, string where, string [] selectionargs)

Query

query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

Description: A cursor is returned Based on the specified Uri. To improve performance, it is best to follow the following suggestions: 1) for the returned results, use the projection parameter to specify the fields contained in the record to avoid the retrieval of a large number of unused fields, waste of resources; 2) use phone =? In the selection parameter? The question mark expression to specify the value of the field in the search condition. Do not use the specified value (such as phone = '000000'), which is conducive to the system cache of the query.

Parameters:

UriIndicates the URI used to obtain the content. For example, the address book is phone. content_uri.

ProjectionField name array. For example, to obtain the display name, phone number, Avatar ID, and contact ID of a contact, the value is:

new String[] { Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID }

SelectionQuery condition string, similar to the WHERE clause in an SQL statement, for example: display_name =? And number =?

SelectionargsQuestion mark (?) in the selection parameter (?) Array of corresponding parameter values

SortorderCorresponds to the order by clause of the SQL statement. For example, if the default sorting is used, the value can be null.

The following code reads the mobile phone Address Book information. First, obtain all the information in the address book and obtain multiple mobile phone numbers of each contact through nesting:

Contentresolver = getcontentresolver (); cursor = contentresolver. query (contactscontract. contacts. content_uri, null, null); // move the cursor to the first row of the result set if (cursor. movetofirst () {// index value of the ID column int idcolumnindex = cursor. getcolumnindex (contactscontract. contacts. _ id); // the index value int dispalynamecolumnindex = cursor. getcolumnindex (contactscontract. contacts. display_name); do {// obtain the user ID value string contactid = cursor. getstring (idcolumnindex); // obtain the username value string displayname = cursor. getstring (dispalynamecolumnindex); // get the number of user numbers int phonenumbercount = cursor. getint (cursor. getcolumnindex (contactscontract. contacts. has_phone_number); If (phonenumbercount> 0) {cursor phonenumbercursor = getcontentresolver (). query (contactscontract. commondatakinds. phone. content_uri, null, // obtain the current user number result set contactscontract. commondatakinds. phone. contact_id + "=" + contactid, null, null); // move the cursor to the first line of all number result sets if (phonenumbercursor. movetofirst () {do {// obtain the number string phonenumber = phonenumbercursor. getstring (phonenumbercursor. getcolumnindex (contactscontract. commondatakinds. phone. number);} while (phonenumbercursor. movetonext ();} // close the cursor phonenumbercursor. close () ;}while (cursor. movetonext ();} // close the cursor. close ();

 

Other functions to be continued ......

Related Article

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.