Data sharing of the ContentProvider component in Android

Source: Internet
Author: User

Data sharing of the ContentProvider component in Android

Functions and meanings of ContentProvider:

It is mainly used for external data sharing, that is, using ContentProvider to share the data in the application to other applications for access. Other applications can use ContentProvider to operate on the data in the specified application.

To share data between different applications and exchange data between applications, one application A exposes its own data to other programs using B through ContentProvider, B can use ContentResolver to operate the data exposed by ContentProvider, including adding, deleting, querying, and modifying

1. ContentProvider uses a table to organize data
Regardless of the data source, ContentProvider considers the data as a table and organizes the data into tables.
2. Methods provided by ContentProvider
Public boolean onCreate () is called when ContentProvider is created
Public Cursor query (Uri, String [], String, String [], String) is used to query the ContentProvider of a specified Uri. A Cursor
Public Uri insert (Uri, ContentValues) is used to add data to the ContentProvider of the specified Uri.
Public int update (Uri, ContentValues, String, String []) is used to update data in the ContentProvider of the specified Uri.
Public int delete (Uri, String, String []) is used to delete data from the specified Uri's ContentProvider.
Public String getType (Uri) is used to return the MIME type of the data in the specified Uri.
* If the operated data belongs to the set type, the MIME-type string should start with vnd. android. cursor. dir.
For example, if the Uri of all person records is content: // contacts/person, the returned MIME-type string is "vnd. android. cursor. dir/person ".
* If the data to be operated belongs to a non-set type, the MIME-type string should start with vnd. android. cursor. item.
For example, if the Uri of the person record whose id is 10 is content: // contacts/person/10, the returned MIME-type string should be "vnd. android. cursor. item/person ".
3. Each ContentProvider has a public URI, which indicates the data provided by this ContentProvider. The ContentProvider provided by Android is stored in the android. provider package.

 

II. Introduction to the Uri class

1. Give a name for each resource in the system, for example, call history. Each ContentProvider has a public URI that represents the data provided by the ContentProvider.

For example, Uri: content: // com. hust. uri/words

Uri specifies the ContentProvider to be operated. In fact, you can regard a Uri as a URL. We divide the Uri into three parts.
The first part is "content ://". It can be viewed as "http: //" in the URL ://".
The second part is the host name or authority, which is used to uniquely identify the ContentProvider. External applications need to find it based on this identifier. It can be seen as the host name in the URL, such as "blog.csdn.net ".
The third part is the path name and the resource part, which indicates the data to be operated.

 

For example, content: // com. hust. uri/words

The accessed resources are words/2, which means to access all data in the word data table.

Example: content: // com. hust. uri/words/2

The accessed resource is words/2, which means to access the record with ID 2 in word data

 

For example, content: // com. hust. uri/words/2/word

The accessed resource is words/2, which means to access the word field of the record with ID 2 in word data

Convert a string into a Uri, which is a static method of the Uri class:

Uri uri = Uri. parse ("content: // com. hust. uri/contact ")

 

2. Introduction to the UriMatcher class

Because Uri represents the data to be operated, we often need to parse the Uri and obtain data from the Uri. The Android system provides two tool classes for Uri operations: UriMatcher and ContentUris.

The UriMatcher class is used to match the Uri:

A, void addURI (String authority, String path, int code) registers Uri with UriMatcher, authority and path to form A Uri. code indicates the ID code of the Uri.

B, int match (Uri uri); Judge the ID code of the specified Uri based on the previously registered Uri

 

UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);matcher.addURI("com.hust.uri","words",1); matcher.addURI("com.hust.uri","words/#",2); 
Com. hust. uri/words/# indicates that all data Uris under words are expressed as 2

 

The matching result is as follows:

 

Matcher. match (Uri. parse ("content: // com. hust. uri/words "); // returns the ID code 1matcher. match (Uri. parse ("content: // com. hust. uri/words/2 "); // returns the identifier code 2matcher. match (Uri. parse ("content: // com. hust. uri/words/10 "); // return ID code 2
After registering the Uri to be matched, you can use sMatcher. the match (uri) method matches the input Uri. If it matches, the matching code is returned. The matching code is the third parameter passed in by calling the addURI () method.

 

3. usage of the ContentUris class

The ontentUris class is used to operate the ID part behind the Uri path. It has two practical methods:
WithAppendedId (uri, id) is used to add the ID part to the path:

Uri resultUri = ContentUris. withAppendedId (uri, 10); // The generated Uri is: content: // com. hust. personprovider/person/10
The parseId (uri) method is used to obtain the ID part from the path:
Uri uri = Uri. parse ("content: // com. hust. uri. personprovider/person/10 ") long personid = ContentUris. parseId (uri); // The result is 10.

3. Develop ContentProvider

 

1. Develop a ContentProvider subclass that inherits ContentProvider and implements query, insert, update, delete, and other methods.

2. register the ContentProvider in the Manifest. xml configuration file and specify its Android: authorities attribute.

 

Public class FirstProvider extends ContentProvider {// this method is called when this ContentProvider is created for the first time @ Overridepublic boolean onCreate () {System. out. println ("=== onCreate method called ==="); return true ;} // The return value of this method indicates the MIME type of the data provided by the ContentProvider @ Overridepublic String getType (Uri uri Uri) {System. out. println ("~~ The getType method is called ~~ "); Return null;} // implement the query method. This method should return the obtained Cursor @ Overridepublic Cursor query (Uri uri, String [] projection, String where, string [] whereArgs, String sortOrder) {System. out. println (uri + "=== query method called ==="); System. out. println ("where parameter:" + where); return null;} // implement the insert method. This method should insert the Uri of the newly inserted record @ Overridepublic Uri insert (Uri uri, contentValues values) {System. out. println (uri + "=== insert method called ="); System. out. println ("values parameter:" + values); return null;} // implements the delete method. This method should return the number of deleted records @ Overridepublic int delete (Uri uri, string where, String [] whereArgs) {System. out. println (uri + "=== delete method called ="); System. out. println ("where parameter:" + where); return 0;} // implements the delete method. This method should return the number of updated records @ Overridepublic int update (Uri uri, contentValues values, String where, String [] whereArgs) {System. out. println (uri + "=== update method called ="); System. out. println ("where parameter:" + where + ", values parameter:" + values); return 0 ;}}

These four methods are used for other applications to call through ContentProvider.
Register ContentProvider:
 
 
Android: authorities attributes must be configured. Specify the Uri of the ContentProvider.

 

The configuration above specifies that the ContentProvider is bound to "content: // org. providers. firstprovider ", which means that when the ContentResovler of other applications executes the query, insert, update, and delete methods to the Uri, it actually calls the query, insert, update, and delete methods of the ContentProvider, when ContentResovler calls a method, it passes the parameter to the method parameter corresponding to ContentProvider.

 

4. Develop contentResovler

The getContentResolver () method is used to obtain the ContentResolver object. After obtaining the ContentResolver object, you can call the query, insert, update, and delete methods. In fact, the query of the ContentProvider corresponding to the specified Uri is called, insert, update, and delete Methods

For example, four buttons on the Interface correspond to the addition, deletion, query, and modification functions:

 

Public class FirstResolver extends Activity {ContentResolver contentResolver; // declare the variable Uri uri = Uri. parse ("content: // org. providers. firstprovider/"); // Uri of FirstProvider @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the system's ContentResolver object contentResolver = getContentResolver ();} public void query (View source) {// call ContentResolver Query () method. // The actual returned result is the returned value of the query () of the ContentProvider corresponding to the Uri. Cursor c = contentResolver. query (uri, null, "query_where", null, null); Toast. makeText (this, "the Cursor returned by the remote ContentProvide is:" + c, Toast. LENGTH_LONG ). show ();} public void insert (View source) {ContentValues values = new ContentValues (); values. put ("name", "fkjava"); // call the insert () method of ContentResolver. // The actual returned result is the returned value of insert () of ContentProvider corresponding to the Uri newUri = contentResolver. insert (uri, values); Toast. makeText (this, "The Uri of the new remote ContentProvide inserted record is:" + newUri, Toast. LENGTH_LONG ). show ();} public void update (View source) {ContentValues values = new ContentValues (); values. put ("name", "fkjava"); // call the ContentResolver update () method. // Actually, the returned value is int count = contentResolver of the ContentProvider update () corresponding to the Uri. update (uri, values, "update_where", null); Toast. makeText (this, "the number of remote ContentProvide update records is:" + count, Toast. LENGTH_LONG ). show ();} public void delete (View source) {// call the delete () method of ContentResolver. // The actual returned result is the return value of delete () of ContentProvider corresponding to the Uri int count = contentResolver. delete (uri, "delete_where", null); Toast. makeText (this, "the number of records deleted by the remote ContentProvide is:" + count, Toast. LENGTH_LONG ). show ();}


 

V. Relationship between ContentProvider and ContentResolver

ContentResolver specifies CRUD and other data operations for the specified Uri, but the Uri is not a real data center. Therefore, these CRUD operations will be delegated to the ContentProvider corresponding to the Uri. Generally, application A performs CRUD operations through ContentResolver. The Uri parameters must be specified for these operations. The Android system then finds the corresponding ContentProvider Based on the Uri (the ContentProvider usually belongs to application B ), contentProvider is responsible for implementing the CRUD method and adding, deleting, modifying, and querying the underlying data, so that application A can access and modify the data of application B.

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.