Description: The article is only for my study record.
1. Understand the meaning:
ContentProvider: The content provider that operates the data as a table. the main implementation of data sharing between applications, operating system Local data (including SMS, audio, video, database).
Contentobserver: Content Watcher, monitor data changes.
2. How to use:
ContentProvider:
1) Step: New MyProvider class inherits ContentProvider class; register URI; override method (OnCreate, query, Bulkinsert, INSERT, delete, update); in Androidmanife St.xml configuration of provider;
2) Code:
Public class myprovider extends contentprovider {private static urimatcher matcher = new urimatcher (urimatcher.no_match);p Rivate static final int INSERT = 1;private static final int UPDATE = 2;private static final int delete = 3;private static final int query = 4;private static final int INSERTS = 5;private DBHelper dbopenhelper;private string tag = "Provider";static {// For urimatcher registration si four Urimatcher.adduri (picture.authority, "Insert", insert); Matcher.adduri ( picture.authority, "Update", update), Matcher.adduri (picture.authority, "Delete", delete); Matcher.adduri (picture.authority, "Query", query); Matcher.adduri (picture.authority, "inserts", inserts);//Description picture.authority: Represents the contentprovider you want to operate, generally to operateis named for the package name of the entity class. } @Overridepublic boolean oncreate () { // Gets the database helper class Dbopenhelper = dbhelper.getinstance (GetContext ());D atabasemanager.initializeinstance ( Dbopenhelper); return true;} @Overridepublic cursor query (uri uri, string[] projection, string selection , String[] selectionargs, string sortorder) {// todo auto-generated method stubint code = matcher.match (URI); cursor c = null;switch (Code) {case query:sqlitedatabase database = dbopenhelper.getreadabledatabase (); C = database.query ("Picture", projection, Selection, selectionargs,null, null, sortorder); C.setnotificationuri (GetContext (). Getcontentresolver (), uri);D atabasemanager.getinstance (). CloseDatabase (); Case urimatcher.no_match: LOG.I (tag, "------------unknown uri" + uri); RetuRn c;} @Overridepublic string gettype (Uri uri) {// todo auto-generated method stubreturn null;} @Overridepublic uri insert (uri uri, contentvalues values) {int code = matcher.match (URI);switch (code) {case INSERT:SQLiteDatabase database = Databasemanager.getinstance (). OpenDatabase ();// thread-safe concurrency Operations Database try {// Get DB instance Database.insert (" Picture ", null, values); GetContext (). Getcontentresolver (). Notifychange (uri, null);} catch (Exception ex) {log.i (tag, "insert ex=====" + ex.tostring ());} finally {databasemanager.getinstance (). CloseDatabase ();} BREAK;CASE&NBSP;URIMATCHER.NO_MATCH:LOG.I (tag, "------------unknown uri" + uri); Return null;} /** * BULK Insert */@Overridepublic int bulkinsert (uri uri, contentvalues[] Values) {int code =&Nbsp;matcher.match (URI);switch (code) {case INSERTS:SQLiteDatabase database = Databasemanager.getinstance (). OpenDatabase ();// thread-safe concurrency Operations Database try {// Get DB instance Int numvalues = 0;numValues = values.length;for (int i = 0; i < numvalues; i++) {contentvalues value = values[i];d atabase.delete ("Picture", " name = ? ", New string[] { string.valueof (Value.get (" name ")) }); LOG.I (TAG, "Name===== values" + string.valueof (Value.get ("name")));d Atabase.insert ("Picture", Null, values[i]); LOG.I (tag, "value insert====" + i); GetContext (). Getcontentresolver (). Notifychange (uri, null);} catch (Exception ex) {log.i (tag, "insert ex=====" + ex.tostring ());} finally {databasemanager.getinstance (). CloseDatabase ();} BREAK;CASE&NBSP;URIMATCHER.NO_MATCH:LOG.I (tag, "------------unknown uri" + uri); return 0;} @Overridepublic int delete (uri uri, string selection, string[] Selectionargs) {int code = matcher.match (URI);switch (code) {case delete: Sqlitedatabase database = databasemanager.getinstance (). OpenDatabase (); int i = 0 ; Try {i = database.delete ("Picture", selection, selectionargs);if (i > 0) {getcontext (). Getcontentresolver (). Notifychange (Uri, null);}} catch (Exception ex) {log.i (tag, "delete ex====" + ex.tostring ());} finally {// database.close ();D atabasemanager.getinstance (). CloseDatabase ();} RETURN&NBSP;I;CASE&NBSP;URIMATCHER.NO_MATCH:LOG.I (tag, "------------unknown uri" + uri); return 0;} @Overridepublic int update (Uri uri, contentvalues values, string selection, String[] selectionargs) {// TODO Auto-generated method stubint code = Matcher.match (URI);switch (code) {case UPDATE:// SQLiteDatabase database = Dbopenhelper.getreadabledatabase (); Sqlitedatabase database = databasemanager.getinstance (). OpenDatabase (); int i = 0 ; Try {i = database.update ("Picture", values, selection, selectionargs);if ( i != 0) {getcontext (). Getcontentresolver (). Notifychange (Uri, null);}} catch (Exception ex) {log.i (tag, "update ex===" + ex.tostring ());} finally {// database.close ();D atabasemanager.getinstance (). CloseDatabase ();} RETURN&NBSP;I;CASE&NBSP;URIMATCHER.NO_MATCH:LOG.I (tag, "------------unknown uri" + uri); return 0;}} Add the following methods in the database operations class. Single Data add public static void addpicture (picture picture) {uri uri = uri.parse ("content://" + Picture.AUTHORITY + "/insert"); Contentvalues values = new contentvalues ();//Package Data Values.put ("name", picture.name); Appapplication.getinstance (). Getcontentresolver (). Insert (uri, values);} Multiple Data add public static void addpictures (list<picture> pictures) {Uri uri = uri.parse ("content://" + Picture.AUTHORITY + "/inserts"); Contentvalues[] values = new contentvalues[pictures.size ()];for (int i = 0; i < pictures.size (); i++) {picture picture = pictures.get ( i); Contentvalues value = new contentvalues (); Value.put ("name", picture.name); values[i ] = value;} LOG.I (tag, "addpictures_values_size=====" + values.length); Appapplication.getinstance (). Getcontentresolver (). Bulkinsert (uri, values);} Delete Operation public static Void deletepicturetag () {uri uri = uri.parse ("content://" + picture.authority + "/delete"); Int i = appapplication.getinstance (). Getcontentresolver (). Delete (uri, "wherename = ?", new string[] { "Value" });} Modify Operation Public static void updatepicture (Picture picture) {Uri uri = Uri.parse ("content://" + Picture.AUTHORITY + "/update"); Contentvalues values = new contentvalues (); Values.put ("Tagstatus", picture.tagStatus) ; Int i = appapplication.getinstance (). Getcontentresolver (). Update (uri, values, " wherename = ? ", new string[] { picture.name }); Configuring provider<provider in Androidmanifest.xml android:name= "Com....provider. MyProvider "//Package name android:authorities= "Com....provider.myprovide" />
Contentobserver:
1) Step: Register and unregister contentobserver; add Contentobserver method.
2) Code:
The URI corresponds to the URI registered in provider Uri uri = uri.parse ("content://" + picture.authority + "/insert");//Monitor add Getcontentresolver (). Registercontentobserver (Uri, true, cob); Uri uri_inserts = uri.parse ("content://" + Picture.AUTHORITY+ "/inserts");// Monitor bulk Add Getcontentresolver (). Registercontentobserver (Uri_inserts, true, cob); Uri uri_update = uri.parse ("content://" + Picture.AUTHORITY + "/update");// Monitoring modifies Getcontentresolver (). Registercontentobserver (Uri_update, true, cob); Getcontentresolver (). Unregistercontentobserver (COB);//method of destroying//monitoring data changes in OnDestroy private contentobserver cob = new contentobserver (New handler ()) {@Overridepublic boolean deliverselfnotifications () {return super.deliverselfnotifications ();} @Overridepublic void onchange (Boolean selfchange) {super.onchange (selfchange);// Make the appropriate action when changing the data ...}
Android Contentprovider+contentobserver