1. Introduction to contentprovider
Contentprovider is translated as "content provider ";
Definition: This application contains some methods for external access. Other applications can call this method. For example, if application a creates a database named "test. DB ", which is private by default. That is, other applications cannot operate on the database. However, if application a uses contentprovider, other applications can access the database;
Purpose: share data with other applications;
Note: Like activity, it is one of the components of Android. To create a component, you must set it in androidmanifest. xml;
Benefits: provides a unified insert, update, delete, query method to operate on any data;
II. Introduction to Uri
Uri: similar to the URI we used previously. However, Uri is used to determine how to operate data based on the uri and the called method. For example:
Uri = ".../person". If the insert method is called, a person record needs to be inserted;
Note the following when using the URI in contentprovider:
(1) start with content;
(2) the mode is content: // authorities/path. Authorities are similar to a domain name or IP address and are used to identify which contentprovider to operate. Path indicates a specific operation;
Example:
Content: // org. xiazdong. providers. personprovider/person indicates to call the "org. xiazdong. providers. personprovider" method to operate on the person data;
Supplement: contenturis helper class
Uri uri = contenturis.Withappendid(URI Param, int ID); // Add an ID for a URI
For example, if Param = "content: // authorities/person" and ID = 10, uri = "content: // authorities/person/10 ";
Long id = contenturis.Parseid(URI); // extract the last ID in the URI
For example, uri = "content: // authorities/person/10", the returned id = 10;
Iii. Brief description of contentprovider development steps
1. Create a class and inherit contentprovider, such as personprovider;
2. Set in androidmanifest. xml:
<Provider Android: Name = ". personprovider" Android: Authorities = "org. xiazdong. Provides. personprovider"/>
3. Define urimatcher,
Private urimatcher matcher = new urimatcher (urimatcher. no_match); // create a URI matcher. The parameter is the return value when the mismatch occurs.
Use matcher. adduri ("authorities", "path", Code) in oncreate; // Add a matched URI. If yes, matcher. Match (URI) returns code;
If you want to match: Content: // authorities/path/number, matcher. adduri ("authorites", "path/#", Code );
4. Rewrite:
Oncreate (): used to prepare operation data;
Insert: insert data. The URI of the inserted record is returned;
Update: update data. The number of records affected by the operation is returned;
Delete: delete data. The number of records affected by the operation is returned;
Query: query data. cursor is returned;
GetType: indicates the record type. If the operation set is set,Vnd. Android. cursor. dirIf the operation is not a set, it must startVnd. Android. cursor. ItemFor exampleVnd. Android. cursor. DIR/person
5. External call:
Contentresolver resolver = This. getcontext (). getcontentresolver ();
Resolver. insert ();
Resolver. Update ();
Resolver. Delete ();
Resolver. Query ();
4. Application Instances
Androidmanifest. xml
<provider android:name=".PersonProvider"android:authorities="org.xiazdong.provides.personprovider" ></provider>
Personprovider. Java
Package Org. xiazdong. DB; 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 databasehelper helper; private sqlitedatabase dB; private urimatcher match ER = new urimatcher (urimatcher. no_match); @ overridepublic Boolean oncreate () {helper = new databasehelper (this. getcontext (); // match: Content: // Org. xiazdong. provides. personprovider/person, the return value is 1matcher. adduri ("org. xiazdong. provides. personprovider "," person ", 1); // match: Content: // Org. xiazdong. provides. personprovider/person/number. The return value is 2matcher. adduri ("org. xiazdong. provides. personprovider "," person/# ", 2); Return True;} @ overridepublic cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder) {DB = helper. getwritabledatabase (); Switch (matcher. match (URI) {Case 1: // query all records return dB. query ("person", projection, selection, selectionargs, null); Case 2: // query a specific record long id = contenturis. parseid (URI); string where = "id =" + ID; If (selection! = NULL &&! "". Equals (selection) {// because selection may contain other where statements, you need to add "and ID =? "Where = where +" and "+ selection;} return dB. query ("person", projection, where, selectionargs, null); default: Throw new illegalargumentexception ("wrong Uri") ;}}/** if the set of operations, it must start with vnd. android. cursor. * If the operation is not a set, it must start with vnd. android. cursor. start with item **/@ overridepublic string GetType (URI) {Switch (matcher. match (URI) {Case 1: Return "Vnd. android. cursor. DIR/person "; Case 2: Return" Vnd. android. cur Sor. item/person ";}}/** values is the inserted data * return: The URI represented by the inserted data **/@ overridepublic URI insert (URI Uri, contentvalues values) {DB = helper. getwritabledatabase (); Switch (matcher. match (URI) {Case 1: Long rowid = dB. insert ("person", null, values); Return contenturis. withappendedid (Uri, rowid); // return the uridefault: Throw new illegalargumentexception ("wrong Uri");} @ overridepublic int Delete (URI Uri, string Sele Ction, string [] selectionargs) {DB = helper. getwritabledatabase (); Switch (matcher. match (URI) {Case 1: Return dB. delete ("person", selection, selectionargs); Case 2: // delete a specific ID record long id = contenturis. parseid (URI); string where = "id =" + ID; If (selection! = NULL &&! "". Equals (selection) {Where + = "and" + selection;} return dB. delete ("person", where, selectionargs); default: Throw new illegalargumentexception ("wrong Uri") ;}@ overridepublic int Update (URI Uri, contentvalues values, string selection, string [] selectionargs) {DB = helper. getwritabledatabase (); Switch (matcher. match (URI) {Case 1: Return dB. update ("person", values, selection, selectionargs); Case 2: // update a specific ID Record long id = contenturis. parseid (URI); string where = "id =" + ID; If (selection! = NULL &&! "". Equals (selection) {Where + = "and" + selection;} return dB. update ("person", values, where, selectionargs); default: Throw new illegalargumentexception ("wrong Uri ");}}}
Create a test class as follows:
Contentprovidertest. Java
Package Org. xiazdong. DB. test; import Org. xiazdong. DB. domain. person; import android. content. contentresolver; import android. content. contentvalues; import android. database. cursor; import android.net. uri; import android. test. androidtestcase; import android. util. log; public class contentprovidertest extends androidtestcase {public void testinsert () throws exception {// inserts a record URI with "name = YYY, age = 100. parse ("content: // Org. xiazdong. provides. personprovider/person "); contentresolver resolver = This. getcontext (). getcontentresolver (); contentvalues values = new contentvalues (); values. put ("name", "yyy"); values. put ("Age", 100); resolver. insert (Uri, values);} public void testupdate () throws exception {// update id = 5 Records: Name = YYY, age = 100uri uri = Uri. parse ("content: // Org. xiazdong. provides. personprovider/person/5 "); contentresolver resolver = This. getcontext (). getcontentresolver (); contentvalues values = new contentvalues (); values. put ("name", "yyy"); values. put ("Age", 100); resolver. update (Uri, values, null, null);} public void testdelete () throws exception {// Delete record URI uri = URI with ID = 11. parse ("content: // Org. xiazdong. provides. personprovider/person/5 "); // Delete the record contentresolver resolver = This. getcontext (). getcontentresolver (); resolver. delete (Uri, null, null);} public void testquery () throws exception {// insert all records and display URI uri = Uri. parse ("content: // Org. xiazdong. provides. personprovider/person "); // query all records contentresolver resolver = This. getcontext (). getcontentresolver (); cursor = resolver. query (Uri, null, null); While (cursor. movetonext () {person = new person (cursor. getint (cursor. getcolumnindex ("ID"), cursor. getstring (cursor. getcolumnindex ("name"), cursor. getint (cursor. getcolumnindex ("Age"); log. V ("contentprovider", person. tostring ());}}}