The examples in this article describe how Android programming uses content providers to store them. Share to everyone for your reference, specific as follows:
The primary role of content providers (ContentProvider) is to share data externally, and if data is shared externally by content providers, other applications can query data from content providers, update data, delete data, add data, and if you use the file's operating mode to share data externally, The way of data access can not be unified because of the different ways of storing data, and the APIs of different storage methods are not the same, if the content providers share the data externally, they will unify the data access mode. Use a unified API to access shared data.
To create a content provider step
1. Creating content providers requires inheriting Android.content.ContentProvider
2. To be configured in the manifest file:
<!--android:name: Specifies the class name of the content provider, Android:authorities: calling content ... Name, random take-->
<provider android:name= ". Personprovider "android:authorities=" Cn.test.providers.personprovider "/>
Main methods of ContentProvider class
Copy Code code as follows:
public boolean onCreate ()
This method is invoked after ContentProvider is created, and when Android is powered on, ContentProvider is created the first time another application accesses it.
Copy Code code as follows:
Public Uriinsert (URI Uri, contentvalues values)
This method is used for external applications to add data to ContentProvider.
Copy Code code as follows:
public int Delete (URI Uri, String selection,string[] selectionargs)
This method is used for external applications to delete data from ContentProvider.
Copy Code code as follows:
public int update (URI uri, contentvalues values, StringSelection, string[] selectionargs)
This method is used for external applications to update data in ContentProvider.
Copy Code code as follows:
Public cursorquery (Uri Uri, String[]projection, string selection, string[] Selectionargs, string sortOrder)
This method is used for external applications to obtain data from ContentProvider.
Example:
Content provider class, to realize data deletion, change and check
public class Personprovider extends ContentProvider {//Create tool class implement URI match private static final urimatcher MATCHER = new Ur
Imatcher (Urimatcher.no_match);
private static final int PERSONS = 1;
private static final int person = 2;
static{Matcher.adduri ("Cn.test.providers.personprovider", "person", PERSONS);
Matcher.adduri ("Cn.test.providers.personprovider", "person/#", person);
Private Dbopenhelper dbopenhelper = null;
@Override public boolean onCreate () {dbopenhelper = new Dbopenhelper (GetContext ());
return true; @Override public Cursor query (URI uri, string[] projection, string selection, string[] Selectionargs, string s
Ortorder) {Sqlitedatabase db = Dbopenhelper.getreadabledatabase (); Switch (Matcher.match (URI)) {case PERSONS://Get all the data in the person table/person return db.query (' person ', projection, se
Lection, Selectionargs, NULL, NULL, sortOrder); Case PERSON://Get data for the specified ID in the person table/person/20 Long id = CONTENTURIS.PArseid (URI);
String where = "personid=" + ID; if (selection!=null &&! "".
Equals (Selection.trim ())) {where = "and" + selection;
return db.query ("person", projection, where, Selectionargs, NULL, NULL, sortOrder);
Default:throw new IllegalArgumentException ("Unknown uri:" + uri);
@Override public String getType (URI uri) {//TODO auto-generated method stub return null; @Override public uri Insert (URI uri, contentvalues values) {//Get database Operation instance Sqlitedatabase db = Dbopenhelper.ge
Twritabledatabase (); Switch (Matcher.match (URI)) {case PERSONS://Execute Add, return line number, if the primary key field is self growing, then the line number equals the primary keys ID long rowid = Db.insert ("PE
Rson "," name ", values);
Get the spelled uri uri Inserturi = Contenturis.withappendedid (URI, ROWID);
Notification of data change (changes in the data of the person table) GetContext (). Getcontentresolver (). Notifychange (URI, NULL);
return Inserturi; Default://Unrecognized URI throw new IllegalargumentexCeption ("Unknown uri:" + uri); @Override public int Delete (URI Uri, String selection, string[] selectionargs) {Sqlitedatabase db = Dbopenh
Elper.getwritabledatabase ();
Number of rows affected int num = 0; Switch (Matcher.match (URI)) {case PERSONS://Delete all data in the person table/person num = db.delete ("Person", selection, SEL
Ectionargs);
Break
Case PERSON://delete data for the specified ID in the person table/person/20 Long id = Contenturis.parseid (URI);
String where = "personid=" + ID; if (selection!=null &&! "".
Equals (Selection.trim ())) {where = "and" + selection;
num = db.delete ("Person", where, Selectionargs);
Break
Default:throw new IllegalArgumentException ("Unknown uri:" + uri);
return num; @Override public int update (URI uri, contentvalues values, String selection, string[] selectionargs) {Sqlitedat
Abase db = Dbopenhelper.getwritabledatabase ();
int num = 0;
Switch (Matcher.match (URI)) { Case PERSONS://Update all data in the person table/person num = db.update ("Person", values, selection, Selectionargs);
Break
Case PERSON://Update data for the specified ID in the person table/person/20 Long id = Contenturis.parseid (URI);
String where = "personid=" + ID; if (selection!=null &&! "".
Equals (Selection.trim ())) {where = "and" + selection;
num = db.update ("Person", values, where, Selectionargs);
Break
Default:throw new IllegalArgumentException ("Unknown uri:" + uri);
return num;
}
}
Access in other projects:
public class Accesscontentproidertest extends Androidtestcase {public void Testinsert () throws throwable{Contentr
Esolver resolver = GetContext (). Getcontentresolver ();
Uri uri = uri.parse ("Content://cn.test.providers.personprovider/person");
Contentvalues values = new Contentvalues ();
Values.put ("name", "Lili");
Values.put ("Phone", "110");
Values.put ("Amount", "3000000000");
Resolver.insert (URI, values);
The public void Testdelete () throws throwable{contentresolver resolver = GetContext (). Getcontentresolver ();
Uri uri = uri.parse ("Content://cn.test.providers.personprovider/person");
int num =resolver.delete (URI, NULL, NULL);
The public void Testupdate () throws throwable{contentresolver resolver = GetContext (). Getcontentresolver ();
Uri uri = uri.parse ("content://cn.test.providers.personprovider/person/65");
Contentvalues values = new Contentvalues ();
Values.put ("Amount", 500); Resolver.update (URI, values, NULL, NULL);
The public void Testquery () throws throwable{contentresolver resolver = GetContext (). Getcontentresolver ();
Uri uri = uri.parse ("content://cn.test.providers.personprovider/person/65");
Cursor Cursor = resolver.query (URI, NULL, NULL, NULL, "PersonID ASC");
while (Cursor.movetonext ()) {String name = cursor.getstring (Cursor.getcolumnindex ("name"));
LOG.I ("Accesscontentprovidertest", name);
}
}
}
I hope this article will help you with your Android programming.