Declaration: server refers to the application that provides the database, and client refers to the application that uses the database.
Permission: if the provider label of the server does not have any permissions, the client does not have to declare any permissions, that is, it can operate on server data.
If the provider label of the server is added with the permission, the client must declare the permission to operate on the server data. In addition, both <permission> and <use-Permission> must be added to the client permissions.
The core code below:
First, the server code
Databasehelper. Java
package com.example.contentproviderserver;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class DatabaseHelper extends SQLiteOpenHelper{private final static String NAME = "test_db";private String CREATE_TALBE1="create table table1(id INTEGER PRIMARY KEY AUTOINCREMENT,data varchar(20));";private String CREATE_TALBE2="create table table2(id2 int,data1 varchar(20));";public DatabaseHelper(Context context) {super(context, NAME, null, 1);// TODO Auto-generated constructor stub}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubdb.execSQL(CREATE_TALBE1);db.execSQL(CREATE_TALBE2);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub}@Overridepublic SQLiteDatabase getReadableDatabase() {// TODO Auto-generated method stubreturn super.getReadableDatabase();}@Overridepublic SQLiteDatabase getWritableDatabase() {// TODO Auto-generated method stubreturn super.getWritableDatabase();}}
The above code is not explained. If you do not understand it, let's take a look at the knowledge of the SQLite database.
Myprovider. Java
Package COM. example. contentproviderserver; 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; import android. util. log; public class myprovider extends contentprovider {Private Static final urimatcher surimatch Er; private databasehelper mdatabasehelper; private string tag = "myprovider"; static {surimatcher = new urimatcher (urimatcher. no_match); surimatcher. adduri ("com. test. provider "," Table1 ", 1); surimatcher. adduri ("com. test. provider "," Table1/# ", 11); surimatcher. adduri ("com. test. provider "," Table2 ", 2); surimatcher. adduri ("com. test. provider "," Table2/# ", 22); // if there are other tables, you can continue to add ...} @ Overridepublic int Delete (URI Uri, string selection, string [] selectionargs) {// todo auto-generated method stubint Code; Code = surimatcher. match (URI); sqlitedatabase DB = mdatabasehelper. getwritabledatabase (); Switch (CODE) {Case 1: DB. delete ("Table1", selection, selectionargs); break; Case 2: DB. delete ("Table2", selection, selectionargs); break; Case 11: log. I (TAG, "matches a single row... "); DB. delete ("Table1", selection, selectionargs); break; Case 22: DB. delete ("Table2", selection, selectionargs); break;} return 0 ;}@ overridepublic string GetType (URI) {// todo auto-generated method stubswitch (surimatcher. match (URI) {Case 1: Case 2: Return "Vnd. android. cursor. DIR/provider "; // match the entire table case 11: Case 22: Return" Vnd. android. cursor. item/provider "; // match a single row. Generally, a single row is not matched, unless the efficiency requirement is high. Default: Return "error"; }}@ overridepublic URI insert (URI Uri, contentvalues values) {// todo auto-generated method stubint code; code = surimatcher. match (URI); sqlitedatabase DB = mdatabasehelper. getwritabledatabase (); long rowid; log. I (TAG, "insert Code --->" + code); Switch (CODE) {Case 1: rowid = dB. insert ("Table1", null, values); If (rowid <0) {log. I (TAG, "insertion failed");} else {URI rowuri = contenturis. withappendedid (Uri, rowid); getcontext (). getcontentresolver (). notifychange (rowuri, null); Return rowuri;} break; Case 2: rowid = dB. insert ("Table2", null, values); If (rowid <0) {log. I (TAG, "insertion failed");} else {URI rowuri = contenturis. withappendedid (Uri, rowid); getcontext (). getcontentresolver (). notifychange (rowuri, null); Return rowuri;} break; Case 11: // dB. insert ("Table1", null, values); break; Case 22: // dB. insert ("Table2", null, values); break;} return NULL ;}@ overridepublic Boolean oncreate () {// todo auto-generated method stubmdatabasehelper = new databasehelper (getcontext (); Return true ;}@ overridepublic cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder) {// todo auto-generated method stubreturn NULL;} @ overridepublic int Update (URI Uri, contentvalues values, string selection, string [] selectionargs) {// todo auto-generated method stubreturn 0 ;}}
Urimather is used to match the URI to find the specified table and operate the table ..
Manifest. xml
Add the <Application> label
<provider android:name="com.example.contentproviderserver.MyProvider" android:authorities="com.test.provider" android:readPermission="com.myprovider.read" android:writePermission="com.myprovider.write" android:enabled="true" > </provider>
Okay. The server code is complete ..
Client code
Mainactivity. Java
Package COM. example. contentproviderclient; import android.net. uri; import android. OS. bundle; import android. app. activity; import android. content. contentvalues; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class mainactivity extends activity implements onclicklistener {button Bt1; button bt2; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); Bt1 = (button) findviewbyid (R. id. bt1); bt2 = (button) findviewbyid (R. id. bt2); bt1.setonclicklistener (this); bt2.setonclicklistener (this) ;}@ overridepublic void onclick (view v) {// todo auto-generated method stubint id = v. GETID (); Switch (ID) {case R. id. bt1: contentvalues CV = new contentvalues (); cv. put ("data", "I love you"); getcontentresolver (). insert (URI. parse ("content: // COM. test. provider/Table1 "), CV); break; case R. id. bt2: contentvalues cv1 = new contentvalues (); cv1.put ("data1", "I love you"); getcontentresolver (). insert (URI. parse ("content: // COM. test. provider/Table2 "), cv1); break ;}}}
Manifest. xml
<uses-permission android:name="com.myprovider.write" /> <uses-permission android:name="com.myprovider.read" /> <permission android:name="com.myprovider.write" /> <permission android:name="com.myprovider.read" />
These four tags are indispensable.
It is very easy to use a contentvalues to assemble data and perform operations. Here we just give an example of insertion. You can test the search and deletion examples by yourself.
Welcome to the discussion. For more information, see the source. Thank you.
Complete source code: http://download.csdn.net/detail/suixiang888/5952209