Storage of Android _ contentprovider

Source: Internet
Author: User

Contentprovider is a mechanism for data sharing between different applications on the Android platform. This mechanism can be used if an application needs to allow other programs to operate on its own data. In addition, this method ignores the underlying data storage implementation. contentprovider provides a unified method for data operations through Uris. The procedure is as follows:

1. Define a contentprovider in the current application.

2. Register this contentprovider in androidmanifest. xml of the current application

3. Other applications use contentresolver and URI to obtain the data of this contentprovider.

 

Contentresolver provides methods such as insert (), delete (), query (), and update. It is used to access data in contentprovider.

Uri is a universal resource identifier, which is divided into four parts: A, B, C, and D:

A: The standard prefix that cannot be changed, including "content: //" and "Tel. When the prefix is "content: //", the data is controlled by a content provider.

B: URI identifier. It is declared through the authorities attribute to define which contentprovider provides the data. For third-party applications, to ensure the uniqueness of the URI identifier, it must be a complete, lowercase class name. For example, "content: // com. Test. Data. myprovider"

C: path, which can be roughly understood as the name of the table in the database to be operated, for example, name in "content: // HX. Android. Text. myprovider/name"

D: If the URI contains the ID of the record to be obtained, the data corresponding to the ID is returned. If no ID exists, all records are returned;

 

The following example shows how to obtain data between applications.

In application a, inherit the contprovider class and override the method.

Public class myprovider extends contentprovider {@ override public int Delete (URI Uri, string selection, string [] selectionargs) {// todo auto-generated method stub return 0 ;} @ override Public String GetType (URI) {// todo auto-generated method stub return NULL;} @ override public URI insert (URI Uri, contentvalues values) {return NULL ;} // initialize a database in create @ override public Boolean oncreate () {sqlitedatabase DB = This. getcontext (). openorcreatedatabase ("test_db.db3", context. mode_private, null); db.exe csql ("create table tab (_ id integer primary key autoincrement, Name text not null)"); contentvalues values = new contentvalues (); values. put ("name", "test"); dB. insert ("tab", "_ id", values); dB. close (); Return true;} // implement the query method @ override public cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder) {sqlitedatabase DB = This. getcontext (). openorcreatedatabase ("test_db.db3", context. mode_private, null); cursor c = dB. query ("tab", null, null); Return C ;}@ override public int Update (URI Uri, contentvalues values, string selection, string [] selectionargs) {// todo auto-generated method stub return 0 ;}}

Declare this contentprovider in its androidmanifest. xml. The authorities attribute defines the URI identifier of this contentprovider.

<provider android:name=".MyProvider" android:authorities="com.test.MyProvider"/>

In application B, use contentresolver to obtain data in contentprovider of program.

Public class mainactivity extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the context CTX = mainactivity. this; // get contentresolver object contentresolver resolver = CTX. getcontentresolver (); // obtain the URI object URI uri = Uri. parse ("content: // COM. test. myprovider "); // obtain data cursor c = resolver. query (Uri, null, null); C. movetofirst (); For (INT I = 0; I <C. getcount (); I ++) {int Index = C. getcolumnindexorthrow ("name"); string src = C. getstring (INDEX); log. D ("", Src); C. movetonext ();}}}

The running result of application B is as follows:

Then observe the structure of the two applications. For example, the red box shows the structure of application A. You can see that there is a database named "test_db.db3, the blue box is the program structure of application B, which does not have any databases for data storage. As shown in the following figure, we can determine that the data queried in application B comes from application.

The above is how contentprovider is used. Compared with SQLite and sharedpreferences, this storage method has obvious complexity, but it is everywhere seen in today's "Cloud, the Data Interaction requirements between programs make the contentprovider storage mechanism an essential part.

The above sample code is only used to demonstrate the use of contentprovider, so many unreasonable points in the program code are not processed.

 

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.