Android Apidemos Sample parsing (App->activity->quickcontactsdemo)

Source: Internet
Author: User

The Quickcontactsdemo sample describes the contacts database that uses the content provider to access the Android system.

Content provider provides a unified interface for sharing data between different applications, and the content provider implements the application code and data layer separation by abstracting the underlying data source. The Android platform provides a corresponding content provider interface for most of the system databases:

Browser: Read and modify Bookmark,browser history or web searches.

Calllog: View or Update call History (incoming or outgoing calls, no calls, etc.)

Contacts: Retrieves, modifies, or stores address books.

Mediastore: Access to the Media Library (including sound, video, images, etc.).

Settings: Access system settings, view or modify Bluetooth settings, ringtones settings, and so on.

Each content provider of the Android system defines a Content_uri function similar to the name of the database. Each context object (such as activity) in Android has a contentresolver,contentresolver that can be content_uri to obtain the corresponding content Provider:

public void OnCreate (Bundle savedinstancestate) {     
 super.oncreate (savedinstancestate);     
 String select = "((" + Contacts.display_name + "notnull) and (" 
 + Contacts.has_phone_number + "=1) and (" 
 + conta Cts. Display_name + "!=") ";     
 Cursor C =     
 getcontentresolver (). Query (Contacts.content_uri,     
 contacts_summary_projection,     
 Select,     
 NULL,     
 Contacts.display_name + "COLLATE localized ASC");     
 Startmanagingcursor (c);     
 Contactlistitemadapter Adapter     
      = new Contactlistitemadapter (this,     
      r.layout.quick_contacts, c);     
 Setlistadapter (adapter);     

}

The Contentresolver object Getcontentresolver (), whose query method is defined as follows:

Public final Cursorquery (Uri uri,string[] projection,string selection,string[] selectionargs,string sortOrder)

URI: The URI of the CONTENT provider that needs to be accessed, such as the URI of the Address Book Contacts.content_uri.

Projection: The column name of the table that needs to be returned, such as NULL, returns all the columns of the table.

Selection: A condition that queries the data table, which is equivalent to the SQL where statement.

Selectionargs: Query parameters equivalent to SQL query criteria?

SortOrder: The order statement that corresponds to a SQL query, the query is sorted, and when it is empty, the default sequence of records is returned.

As you can see, the Content provider is very similar to the database usage. The object returned by query is Cusor, and after the cursor object you can insert, delete, and update the database as you would access the database table.

Startmanagingcursor (c); Let the activity manage the life cycle of the cursor.

In addition, access to content provider also requires the appropriate permissions to properly access, such as reading and writing address book, need to set up in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>

To have access to the communication record.

Note: If you run this example on the emulator, you need to add several contacts to the contacts, otherwise this example is not shown.

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.