Android Developer content provider ContentProvider

Source: Internet
Author: User

2 content providers ContentProvider

I. Introduction of ContentProvider
WhenApplicationInherit the ContentProvider class and override the class's methods for providing data and storing data, and you can share its data with other apps. While other methods can be used to share data externally, data access will vary depending on how the data is stored, such as: usingfileWay to share data, need to file operation read and write data, using Sharedpreferences to share data, need to use sharedpreferencesAPIRead and write data. The advantage of using ContentProvider to share data is thatUnifiedThe way data is accessed.
Ii. Introduction to the URI class
The URI represents the data to manipulate, and the URI mainly contains two pieces of information: 1. ContentProvider to operate, 2. To manipulate what data is in ContentProvider, a URI consists of the following parts:

1.scheme:contentprovider (content Provider) scheme has been defined by Android as: content://.
2. hostname (or authority): used to uniquely identify this contentprovider, external callers can find it based on this identity.
3. Path: can be used to represent the data we want to manipulate, the path should be built according to the business, as follows:
? To manipulate records with ID 10 in the Contact table, you can build such a path:/CONTACT/10
? To manipulate the name field of the record with ID 10 in the Contact table, Contact/10/name
? To manipulate all records in the Contact table, you can build such a path:/contact
The data to be manipulated does not necessarily come fromDatabase, or it can be a file, such as his storage method, as follows:
To manipulate the name node under the contact node in the XML file, you can build such a path:/contact/name
If you want to convert a string to a URI, you can use the parse () method in the Uri class, as follows:
Uri uri = uri.parse ("Content://com.changcheng.provider.contactprovider/contact")
Iii. Introduction to Urimatcher, Contenturist and Contentresolver
Because the URI represents the data to manipulate, we often need to parse the URI and from the URIGetData. AndroidsystemProvides two tool classes for manipulating URIs, Urimatcher and Contenturis. Mastering their use will facilitate ourDevelopmentJob.

Urimatcher: Used to match the URI, it uses the following:
1. First put the URI path you need to match all to the registration, as follows:
The constant Urimatcher.no_match represents a return code (-1) that does not match any path.
Urimatcher urimatcher = new Urimatcher (urimatcher.no_match);
If the match () method matches the Content://com.changcheng.sqlite.provider.contactprovider/contact path, a match code of 1 is returned
Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "Contact", 1);//Add need to match URI, if match will return match code
If the match () method matches the content://com.changcheng.sqlite.provider.contactprovider/contact/230 path, a match code of 2 is returned
Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "contact/#", 2);//#号为通配符

2. After registering a URI that needs to be matched, you can use the Urimatcher.match (URI) method to match the input URI, and if the match returns a matching code, the match code is the third parameter passed in by calling the Adduri () method, assuming that the match content:// Com.changcheng.sqlite.provider.contactprovider/contact path, the matching code returned is 1.

Contenturis: Used to get the ID part after the URI path, it has two more practical methods:
? Withappendedid (URI, id) is used to add the ID portion of the path
? The Parseid (URI) method is used to get the ID part from the path

Contentresolver: When an external application needs to add, delete, modify, and query the data in ContentProvider, you can use the Contentresolver class to get the Contentresolver object, You can use the Getcontentresolver () method provided by the activity. Contentresolver uses the INSERT, delete, update, and query methods to manipulate the data.

Iv. ContentProvider Sample program

Content provider Personprovider needs to inherit the ContentProvider class to implement the unified interface content provided

Package Com.andy.sqlite;import Com.andy.service.dbopenhelper;import Android.content.contentprovider;import Android.content.contenturis;import Android.content.contentvalues;import Android.content.urimatcher;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.net.Uri;public Class Personprovider extends ContentProvider {private dbopenhelper dbopenhelper;//Uri match private static final urimatcher match ER = new Urimatcher (urimatcher.no_match);//mismatch when//define match flag private static final int PERSONS = 1;private static final int Perso N = 2;static {//"Content://andy.sqlite.providers.personprovider/person" matches the item Matcher.adduri (" Andy.sqlite.providers.personprovider "," person ", PERSONS);//" content://andy.sqlite.providers.personprovider/ PERSON/11 "matches the item #代表匹配的数字MATCHER. Adduri (" Andy.sqlite.providers.personprovider "," person/# ", person);} @Overridepublic int Delete (URI Uri, String selection, string[] selectionargs) {sqlitedatabase sqlitedatabase = Dbopenhelp Er.getwritabledatAbase (); int num = 0;switch (Matcher.match (URI)) {Case persons:num = sqlitedatabase.delete (' person ', selection, Selectionargs); break;case person:long rowid = Contenturis.parseid (URI); String where = "PersonID =" + rowid;if (selection! = NULL &&! "). Equals (Selection.trim ())) {where + = selection;} num = sqlitedatabase.delete ("Person", where, Selectionargs); Break;default:throw new IllegalArgumentException ("This is Unknow URI: "+ uri);} return num;} @Overridepublic String getType (Uri uri) {switch (Matcher.match (URI)) {case Persons:return "vnd.android.cursor.dir/ Person ", Case Person:return" Vnd.android.cursor.item/person ";d efault:throw new IllegalArgumentException Unknow URL: "+ uri);}} @Overridepublic uri insert (URI uri, contentvalues values) {sqlitedatabase sqlitedatabase = Dbopenhelper.getwritabledatabase (); switch (Matcher.match (URI)) {case Persons:long rowID = Sqlitedatabase.insert (" Person "," name ", values); Uri Inserturi = Contenturis.withappendedid (URI, rowid); This.getcontExt (). Getcontentresolver (). Notifychange (URI, null); return inserturi;default:throw new IllegalArgumentException (" This is unknow URI: "+ uri);}} @Overridepublic Boolean onCreate () {dbopenhelper = new Dbopenhelper (GetContext ()); return true;} @Overridepublic Cursor query (Uri uri, string[] projection, string selection,string[] Selectionargs, string sortOrder) {SQ Litedatabase sqlitedatabase = dbopenhelper.getreadabledatabase (); switch (Matcher.match (URI)) {case Persons:return Sqlitedatabase.query ("person", projection, Selection,selectionargs, null, NULL, sortOrder); case Person:long rowID = Contenturis.parseid (URI); String where = "PersonID =" + rowid;if (selection! = NULL &&! "). Equals (Selection.trim ())) {where + = selection;} return sqlitedatabase.query ("person", projection, Where,selectionargs, null, NULL, SortOrder);d Efault:throw New IllegalArgumentException ("This is unknow URI:" + uri);}} @Overridepublic int update (URI uri, contentvalues values, String selection,string[] Selectionargs) {Sqlitedatabase sqlitedatabase = dbopenhelper.getwritabledatabase (); int num = 0;switch (Matcher.match (URI)) {case Persons:num = sqlitedatabase.update ("Person", values, Selection,selectionargs); break;case Person:long rowid = Contenturis.parseid (URI); String where = "PersonID =" + rowid;if (selection! = NULL &&! "). Equals (Selection.trim ())) {where + = selection;} num = sqlitedatabase.update ("Person", values, where, Selectionargs); Break;default:throw New IllegalArgumentException ( "This is unknow URI:" + uri);} return num;}}


Android Developer content provider ContentProvider

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.