When an app inherits the ContentProvider class and overrides the method used to provide data and store data, it can share its data with other apps. Previously we have learned the operation mode of the file, by specifying the mode of operation of the file for context.mode_world_readable or context.mode_world_writeable can also share data externally, However, data access will vary depending on how the data is stored, such as: Using XML files to share data externally, XML parsing is required to read and write data, and sharedpreferences sharing data requires the use of the Sharedpreferences API to read and write data. The benefit of using ContentProvider to share data is to unify the way data is accessed.
1. The first step requires inheriting ContentProvider and overriding the following methods:
public class Personcontentprovider extends contentprovider{
public boolean onCreate ()
Public URI insert (URI uri, contentvalues values)
public int Delete (URI Uri, String selection, string[] Selectionargs)
public int update (URI uri, contentvalues values, String selection, string[] Selectionargs)
Public Cursor query (URI uri, string[] projection, string selection, string[] Selectionargs, string sortOrder)
Public String GetType (URI Uri)}
2. The second step requires the configuration of the ContentProvider in Androidmanifest.xml using <provider>, in order for other applications to find the ContentProvider, ContentProvider Using authorities (hostname/domain name) to uniquely identify it, you can think of ContentProvider as a website (imagine, the site is also providing data), authorities is his domain name:
<manifest. >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<provider android:name= ". Userprovider "android:authorities=" Com.example.providerdemo.userProvider "/>
</application>
</manifest>
Note: Once the app inherits the ContentProvider class, we'll refer to this app as the ContentProvider (content provider) later.
The Providerdemo has a DB that provides provider for additional screening and deletion on the periphery. (This can be analogous to the correspondence record)
Oneapp him to the operation.
Provider definition:
/** * */package com.example.providerdemo;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;/** * * @author BAOXING.GBX * @version $Id: Userprovider.java, v 0.1 August 21, 2015 morning 10:25:48 baoxing.gbx EXP $ */public Class Userprovider extends contentprovider{ Private Dbopenhelper Dbopenhelper; private static final Urimatcher MATCHER = new Urimatcher (urimatcher.no_match); private static final int userone = 1; private static final int userlist = 2; static{Matcher.adduri ("Com.example.providerdemo.userProvider", "User", userlist); Matcher.adduri ("Com.example.providerdemo.userProvider", "user/#", Userone); } @Override public Boolean onCreate () {dbopenhelper = new Dbopenhelper (This.getcontext ()); return true; } @Override Public Cursor QuerY (Uri Uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { Sqlitedatabase db = Dbopenhelper.getwritabledatabase (); Switch (Matcher.match (URI)) {case Userone:long key = Contenturis.parseid (URI); String where = "id =" + key; if (selection!=null &&! "". Equals (Selection.trim ())) {where + = "and" + selection; } cursor cursor = db.query ("user", null, where, Selectionargs, NULL, NULL, NULL, NULL); return cursor; Case userlist:cursor = db.query ("user", NULL, NULL, SELECTIONARGS, NULL, NULL, NULL, and NULL); return cursor; Default:throw New IllegalArgumentException ("This is Unknown URI:" + uri); }} @Override public String getType (URI uri) {//TODO auto-generated method stub return null; } @OvErride public uri Insert (URI uri, contentvalues values) {sqlitedatabase db = Dbopenhelper.getwritabledatabase () ; Switch (Matcher.match (URI)) {case Userone:long rowID = Db.insert ("User", "", values);//Primary key value Uri Inserturi = Contenturis.withappendedid (URI, ROWID); This.getcontext (). Getcontentresolver (). Notifychange (URI, NULL);//Send data change notification return Inserturi; Default:throw New IllegalArgumentException ("This is Unknown URI:" + uri); }} @Override public int delete (URI Uri, String selection, string[] selectionargs) {Sqlitedatabase db = d Bopenhelper.getwritabledatabase (); int nnum = 0; Switch (Matcher.match (URI)) {case Userone:long key = Contenturis.parseid (URI); String where = "id =" + key; if (null! = Selection &&! "". Equals (selection)) {where + = "and" + selection; } Nnum = Db.delete ("User", where, Selectionargs); return nnum; Case userlist:nnum = db.delete ("User", selection, Selectionargs); return nnum; Default:throw New IllegalArgumentException ("This is Unknown URI:" + uri); }} @Override public int update (URI uri, contentvalues values, String selection, string[] Selectionargs) { Sqlitedatabase db = Dbopenhelper.getwritabledatabase (); int nnum = 0; Switch (Matcher.match (URI)) {case Userone:long key = Contenturis.parseid (URI); String where = "id =" + key; if (null! = Selection &&! "". Equals (selection)) {where + = "and" + selection; } Nnum = Db.update ("User", values, where, Selectionargs); return nnum; Case UserlisT:nnum = db.update ("User", values, selection, Selectionargs); return nnum; Default:throw New IllegalArgumentException ("This is Unknown URI:" + uri); } }}
DB Operation:
Package Com.example.providerdemo;import Android.content.context;import android.database.sqlite.SQLiteDatabase; Import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.SQLiteOpenHelper; Import Android.nfc.tag;import Android.os.build.version;import Android.util.Log; /** * * @author BAOXING.GBX * @version $Id: Dbopenhelper.java, v 0.1 July 11, 2015 PM 9:39:03 baoxing.gbx EXP $ */public Class Dbopenhelper extends sqliteopenhelper{/** database name */private static final String db_name = "test.db"; private static final int VERSION = 1; private static final String Tag = "Dbopenhelper"; Initialize the database open Appliance Public dbopenhelper (context context) {Super (context, db_name, NULL, VERSION); TODO auto-generated Constructor stub}//@Override public void OnCreate (Sqlitedatabase db) {LOG.I (Tag, "onCreate is called"); Db.execsql ("CREATE table user (ID integer primary key autoincrement, name varchar), sex VArchar (), age integer) "); } @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {log.i (Tag, "Onupgrade be called "); Db.execsql ("DROP TABLE IF EXISTS person"); OnCreate (DB); } }
Oneapp Test
/** * */package com.demo.oneapp.test;import java.util.date;import android. R.integer;import Android.content.contentresolver;import Android.content.contentvalues;import Android.database.cursor;import android.net.uri;import Android.test.androidtestcase;import android.util.Log;/** * * @ Author BAOXING.GBX * @version $Id: Testprovider.java, v 0.1 August 20, 2015 PM 8:50:13 baoxing.gbx EXP $ */public Class Testprov Ider extends Androidtestcase {public void Testadd () {uri uri = uri.parse ("Content://com.example.providerde Mo.userprovider/user "); Contentresolver resolver = This.getcontext (). Getcontentresolver (); for (int i = 0; i < ++i) {contentvalues values = new Contentvalues (); Values.put ("name", "muyou" + i); Values.put ("Sex", "male"); Values.put ("age", + i); Uri uri2 = Resolver.insert (URI, values); System.out.println ("Done:" + uri2); }} public void Testdel () {//Query single URI URI = Uri.parse ("CONTENT://COM.EXAMPLE.PROVIDERDEMO.USERPROVIDER/USER/2"); Contentresolver resolver = This.getcontext (). Getcontentresolver (); cursor cursor = resolver.query (URI, NULL, NULL, NULL, NULL); while (Cursor.movetonext ()) {int id = cursor.getint (cursor.getcolumnindex ("id")); String name = cursor.getstring (Cursor.getcolumnindex ("name")); String sex = cursor.getstring (Cursor.getcolumnindex ("Sex")); int age = Cursor.getint (Cursor.getcolumnindex ("Age")); LOG.I ("Testquery", "id=" + ID + "Name=" + name + "sex=" + Sex + "age=" + age); } URI = Uri.parse ("CONTENT://COM.EXAMPLE.PROVIDERDEMO.USERPROVIDER/USER/2"); Resolver = This.getcontext (). Getcontentresolver (); Resolver.delete (URI, NULL, NULL); Querying a single URI = Uri.parse ("CONTENT://COM.EXAMPLE.PROVIDERDEMO.USERPROVIDER/USER/2"); Resolver = This.getcontext (). Getcontentresolver (); cursor = resolver.query (URI, NULL, NULL, NULL, NULL); while (Cursor.movetonext ()) {int id = cursor.getint (cursor.getcolumnindex ("id")); String name = cursor.getstring (Cursor.getcolumnindex ("name")); String sex = cursor.getstring (Cursor.getcolumnindex ("Sex")); int age = Cursor.getint (Cursor.getcolumnindex ("Age")); LOG.I ("Testquery", "id=" + ID + "Name=" + name + "sex=" + Sex + "age=" + age); }} public void Testupdate () {//query single URI URI = Uri.parse ("Content://com.example.providerdemo.use Rprovider/user/3 "); Contentresolver resolver = This.getcontext (). Getcontentresolver (); cursor cursor = resolver.query (URI, NULL, NULL, NULL, NULL); while (Cursor.movetonext ()) {int id = cursor.getint (cursor.getcolumnindex ("id")); String name = CUrsor.getstring (Cursor.getcolumnindex ("name")); String sex = cursor.getstring (Cursor.getcolumnindex ("Sex")); int age = Cursor.getint (Cursor.getcolumnindex ("Age")); LOG.I ("Testquery", "id=" + ID + "Name=" + name + "sex=" + Sex + "age=" + age); } URI = Uri.parse ("CONTENT://COM.EXAMPLE.PROVIDERDEMO.USERPROVIDER/USER/3"); Resolver = This.getcontext (). Getcontentresolver (); Contentvalues values = new Contentvalues (); Values.put ("name", "cool"); Resolver.update (URI, values, NULL, NULL); Querying a single URI = Uri.parse ("CONTENT://COM.EXAMPLE.PROVIDERDEMO.USERPROVIDER/USER/3"); Resolver = This.getcontext (). Getcontentresolver (); cursor = resolver.query (URI, NULL, NULL, NULL, NULL); while (Cursor.movetonext ()) {int id = cursor.getint (cursor.getcolumnindex ("id")); String name = cursor.getstring (cursor.getColumnIndex ("name")); String sex = cursor.getstring (Cursor.getcolumnindex ("Sex")); int age = Cursor.getint (Cursor.getcolumnindex ("Age")); LOG.I ("Testquery", "id=" + ID + "Name=" + name + "sex=" + Sex + "age=" + age); }} public void Testquery () {//query all URI uri = Uri.parse ("Content://com.example.provi Derdemo.userprovider/user "); Contentresolver resolver = This.getcontext (). Getcontentresolver (); cursor cursor = resolver.query (URI, NULL, NULL, NULL, NULL); while (Cursor.movetonext ()) {int id = cursor.getint (cursor.getcolumnindex ("id")); String name = cursor.getstring (Cursor.getcolumnindex ("name")); String sex = cursor.getstring (Cursor.getcolumnindex ("Sex")); int age = Cursor.getint (Cursor.getcolumnindex ("Age")); LOG.I ("Testquery", "id=" + ID + "Name=" + name + "sex=" + SEx + "age=" + age); }//Query single URI = Uri.parse ("CONTENT://COM.EXAMPLE.PROVIDERDEMO.USERPROVIDER/USER/5"); Resolver = This.getcontext (). Getcontentresolver (); cursor = resolver.query (URI, NULL, NULL, NULL, NULL); while (Cursor.movetonext ()) {int id = cursor.getint (cursor.getcolumnindex ("id")); String name = cursor.getstring (Cursor.getcolumnindex ("name")); String sex = cursor.getstring (Cursor.getcolumnindex ("Sex")); int age = Cursor.getint (Cursor.getcolumnindex ("Age")); LOG.I ("Testquery", "id=" + ID + "Name=" + name + "sex=" + Sex + "age=" + age); } } }
Android ContentProvider Simple Learning