1. Content Provider
SQLite saves each application's own database, which is inaccessible between application databases , and content Provider solves this problem.
It stores data that can be accessed by individual applications, can create content Providerin its own application, or can directly use existing content Provider.
2.ContentResolver
Contentresolver cr=context.getcontentresolver ();//Get the object through the Context
In Android, the app does not have direct access to the content Providerand must be accessed through the methods provided by the Contentresolver interface .
Each Content provider class has only one instance object, which can be accessed by Contentresolver objects in multiple applications.
3.URI (Unified Resource Identifier, unified resource ID)
Each content provider stores the same data as SQLite and stores it as a table , but in addition to the table name, the field name.
Content Provider requires a unique URI (Data_uri)for each table, similar to a URI that indicates a path
String. Form:<scheme>://<authority>/<path>?<query>.
The data table,<scheme> for content provider has been fixed with "content" as a data table for content provider.
<authority>: The class name of the concrete class for inheriting content provider.
<path>: For the table name, as in the following author_table = "Author";
How operations Modify the generation of new URIs
Uri uri=uri.parse("Content://media/images/media");
Uri uri1=contenturis.withappendedid(URI, 2);//used to create a URI that points to an ID record
Uri uri2=uri.withappendedpath(URI, "2");//used to create a URI that points to a table
Note: The first parameter in the method does not end with a "/".
Public Final classPublisher {PrivatePublisher () {} Public Static FinalString author_table = "AUTHOR"; Public Static FinalString book_table = "book"; Public Static FinalString book_author_table = "Book_author"; Public Static FinalUri Content_uri = Uri.parse ("content://" +publisherprovider.provider_name); Public Static FinalUri Book_author_uri =Uri.withappendedpath (Content_uri,"Book_authortable"); Public Static classAUTHORImplementsBasecolumns { Public Static FinalUri Data_uri =Uri.withappendedpath (Content_uri,"Authortable"); Public Static FinalString NAME = "Author_name"; Public Static FinalString address = "Address"; Public Static FinalString phone = "Phone"; Public Static FinalString order_by = "Author_name DESC"; } Public Static classBookImplementsBasecolumns { Public Static FinalUri Data_uri =Uri.withappendedpath (Content_uri,"Booktable"); Public Static FinalString NAME = "Book_name"; Public Static FinalString author_id = "author_id"; Public Static FinalString publish_year = "Public_year"; Public Static FinalString order_by = "Book_name DESC"; }}
4. Content Provider
The content provider that comes with Android are all in the "Android.provider" package and are commonly used in the following categories:
Browser: Reading or modifying tags, browsing history and web search records
Calllog: Read or modify call history
Contracts: reading or modifying contact information
Mediastore: provides read/write control over multimedia files (audio, video, and pictures) and can be accessed globally
Settings: Read or modify settings information.
5. Declaring content Provider
Declare in the manifest file:
<provider android:name= ". Publisherprovider "
Android:multiprocess= "false"
Android:authorities= "Publisherprovider"
/>
Both the authorities tag and the name tag inherit the class name of the content provider concrete class.
loading mechanism of 5.1 Content provider
When Android starts, the platform uses the Acquireprovider () method to parse the provider declaration in the manifest file.
Authority field and automatically loads all the corresponding content Provider, and the Multiprocess property value determines
Whether the content provider can be created in multiple application processes.
Multiprocess:false refers to other applications that want to use the content Provider, only through interprocess communication (IPC)
method to invoke the content provider object. The effect synchronizes the object for a synchronous operation
Multiprocess:true means that other apps can create an instance of the content provider in their own app process
To use it again, the effect is out of sync.
6. Create a custom content Provider
Member variables: Open database connection Openhelper, database object Db,uri match, and its constant value,
such as int code, String type
Sqlitedatabase DB;
Publisherdatabasehelper Mhelper
Urimatcher Urimatcher
Methods to overwrite:
The Crud method is actually encapsulating the database object in DB method, just need URI match to parse URI parameter, in the method of depositing into DB
public boolean onCreate ()
Public String GetType (URI uri)
Public Cursor query (URI uri, string[] projection,..)
Public URI insert (URI uri, contentvalues values)
public int Delete (URI Uri, String where, string[] Whereargs)
public int update (URI uri, contentvalues values,..)
7.DAO Connection and realization
Manipulate the custom content Provider by using the methods provided in the Contentresolver object
Public classContentproviderdaoImplementsPubliserdao {Private StaticContentproviderdao instance; Privatecontext Context; PrivateContentresolver Contentresolver; PrivateContentproviderdao (Context ctx) {context=CTX; Contentresolver=Context.getcontentresolver (); } Public Static synchronizedContentproviderdao getinstance (Context ctx) {if(Instance = =NULL) Instance=NewContentproviderdao (CTX); returninstance; } Public voidDeleteauthor (Longauthor_id) {Contentresolver.delete (AUTHOR). Data_uri, author._id+ " = " +author_id,NULL); } PublicCursor Getauthorbyid (LongID) {returnContentresolver.query (AUTHOR. Data_uri,NULL, author._id + "=" + ID,NULL,NULL); } PublicCursor getauthors () {returnContentresolver.query (AUTHOR. Data_uri,NULL,NULL,NULL, Publisher.AUTHOR.ORDER_BY); } PublicCursor GetBooksByAuthor (Longauthor_id) { returnContentresolver.query (Publisher.book_author_uri,NULL, book. author_id+ "=" + author_id,NULL,NULL); } Public voidInsertauthor (string name, string address, String phone) {contentvalues values=Newcontentvalues (); Values.put (Publisher.AUTHOR.NAME, NAME); Values.put (Publisher.AUTHOR.ADDRESS, ADDRESS); Values.put (Publisher.AUTHOR.PHONE, PHONE); Contentresolver.insert (AUTHOR. Data_uri, values); } Public voidUpdateauthor (LongID, string name, string address, String phone) {URI Uri=Contenturis.withappendedid (AUTHOR. Data_uri, id); Contentvalues Values=Newcontentvalues (); Values.put (Publisher.AUTHOR.NAME, NAME); Values.put (Publisher.AUTHOR.ADDRESS, ADDRESS); Values.put (Publisher.AUTHOR.PHONE, PHONE); Contentresolver.update (URI, values,NULL,NULL); } Public voiddeleteauthors (String where) {Contentresolver.delete (AUTHOR. Data_uri, where,NULL); }}
android-Data Persistence storage content Provider