Android ApiDemos example (10): App-& gt; Activity-& gt; QuickC

Source: Internet
Author: User

The quick contactsdemo example shows how to use Content Provider to access the Contacts Database of Android system.

Content Provider provides a unified interface for sharing data between different applications. by abstracting the underlying data source, Content Provider separates application code from the data layer. The Android platform provides the corresponding Content Provider interface for most system databases:

Browser: Read and modify Bookmark, Browser history, or Web Searches.
CallLog: view or update Call History (Call or Call, missed Call, etc)
Contacts: searches, modifies, or stores the address book.
MediaStore: access the media repository (including sound, video, and image ).
Settings: Access System Settings, view or modify Bluetooth Settings, and set ringtones.
Each Content Provider in the Android system defines a CONTENT_URI, which is similar to the database name. Each Context object (such as Activity) in Android contains a ContentResolver. ContentResolver can obtain the corresponding Content Provider based on CONTENT_URI:

 

[Java]
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
String select = "(" + Contacts. DISPLAY_NAME + "NOTNULL) AND ("
+ Contacts. HAS_PHONE_NUMBER + "= 1) AND ("
+ Contacts. 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 );

}
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
String select = "(" + Contacts. DISPLAY_NAME + "NOTNULL) AND ("
+ Contacts. HAS_PHONE_NUMBER + "= 1) AND ("
+ Contacts. 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 );
 
}
GetContentResolver () obtains the ContentResolver object. Its Query method is defined as follows:

Public final Cursorquery (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder)

Uri: URI of the Content Provider to be accessed. For example, the URI of the address book is Contacts. CONTENT_URI.
Projection: name of the column in the table to be returned. If it is NULL, all columns in the table are returned.
Selection: the condition for querying data tables, which is equivalent to the SQL Where statement.
SelectionArgs: the query parameter equivalent to the SQL query condition?
SortOrder: it is equivalent to the Order Statement of SQL query. If it is null, the default Order of records is returned.
It can be seen that the usage of Content Provider and database is very similar. The object returned by the query is Cusor. After the Cursor object is available, you can insert, delete, and update the database just like accessing the database table.

StartManagingCursor (c); let the Activity manage the life cycle of cursor.

In addition, proper permissions are required for access to the Content Provider. For example, to read and write the address book, you need to set in AndroidManifest. xml:

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

In order to have the permission to access the address book.

 

Note: If you run this example on the simulator, you need to add several Contacts in Contacts; otherwise, this example is not displayed.

Author: mapdigit

 

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.