1)ContentProviderIntroduction
This class is used to provide data and methods for storing data, and it is possible to share its data with other applications. Although the use of other methods can also share data, but the way data access will vary depending on how the data is stored, such as: the use of file-based data sharing, the need for file operations read and write data;sharedpreferencesto share data, you need to usesharedpreferences APIread and write data. and usingContentProviderthe benefit of sharing data is that it unifies the way data is accessed.
2)eachContentProviderall have a publicURI, thisURIused to represent thisContentProviderThe data provided. Androidprovided by theContentProviderare stored inAndroid.providerPackage,Uriclass,such as:
Uri uri = uri.parse ("Content://com.changcheng.provider.contactprovider/contact")
3)ContentProviderthe methods provided
Query: Query
Insert: Inserting
Update: Update
Delete: Delete
GetType: Get Data type
OnCreate: callback function to invoke when creating data
in the Content Provider the query string used in this is different from the standard SQL Enquiry
Case: read the contact data as an example
Create a new project, first in to set the ability to read contacts in Androidmanifest.xml:
Permission in ' Add ', UserPermission
Android.permission.READ_CONTACTS
actually Androidmanifest.xml added a line:
<uses-permissionandroid:name="Android.permission.READ_CONTACTS"/>
2) Read the name of the contact person
protectedvoid onCreate (Bundle savedinstancestate) {
Super. OnCreate (savedinstancestate);
Setcontentview (r.layout. Activity_main );
Cursor c=getcontentresolver (). Query (contactscontract.contacts. null null null null
while(C.movetonext ()) {System. out . println (">>>>>>>>>"+c.getstring (C.getcolumnindex ( Contactscontract.contacts. Display_name )));
}
Reference:
Http://www.cnblogs.com/devinzhang/archive/2012/01/20/2327863.html
http://xiechengfa.iteye.com/blog/1415829
This article is from "Blue Sea Tactics" blog, please make sure to keep this source http://wanxl.blog.51cto.com/2129901/1592776
10-Day Learning Android Development (3-2)-component ContentProvider