Use ContentProvider shared data "component must be placed under Application package or sub package, also need to configure in the manifest file, like Gallery, SMS, address Book is content provider"
When you apply an inherited ContentProvider class and override the class's methods for providing data and storing data, you can share its data with other applications. Previously we have studied the operation mode of the file, by specifying the operation mode of the file for context.mode_world_readable or context.mode_world_writeable can also share data externally, However, the way of data access will vary depending on the way data is stored, such as: Using XML files to share data externally, XML parsing to read and write data, using Sharedpreferences to share data, need to use SHAREDPREFERENCESAPI to read and write data. The benefit of using ContentProvider to share data is to unify the way data is accessed.
When an application needs to share data externally through ContentProvider, the first step is to inherit ContentProvider and rewrite the following methods:
"The external unification knows these several methods, concrete how to do is my own matter."
public class Personcontentprovider extends contentprovider{
Public Booleanoncreate ()
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, stringselection, string[] Selectionargs, String sortOrder)
Public String GetType (URI Uri)}
The second step is to configure the ContentProvider in Androidmanifest.xml using <provider>, in order for other applications to find the ContentProvider, ContentProvider used authorities (hostname/domain name) to its unique identification, you can take ContentProvider as a website (think, the site is also providing data), authorities is his domain name:
<manifest. >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<providerandroid:name= ". Personcontentprovider "android:authorities=" Cn.itcast.providers.personprovider "/>
</application>
</manifest>
Note: Once the application inherits the ContentProvider class, we will call this application ContentProvider (content Provider) later.
The URI represents the data to be manipulated, and the URI contains two pieces of information: 1 The contentprovider,2 that is required to operate on what data in ContentProvider, a URI consists of the following parts:
ContentProvider (content Provider) scheme has been set by Android, and scheme is: content://
The host name (or authority) is used to uniquely identify this contentprovider, and the external caller can find it based on the identity.
Path can be used to represent the data we want to manipulate, and the path should be built according to the business, as follows:
To manipulate records with ID 10 in the person table, you can build such a path:/PERSON/10
To manipulate the name field of a record with ID 10 in the person table, Person/10/name
To manipulate all records in the person table, you can build such a path:/person
To manipulate the records in the XXX table, you can build such a path:/xxx
Of course, the data to be manipulated does not necessarily come from the database, or other storage methods such as file, XML, or network, as follows:
To manipulate the name node under the person node in the XML file, you can build such a path:/person/name
If you want to convert a string to a URI, you can use the parse () method in the Uri class, as follows:
Uri uri = uri.parse ("Content://cn.itcast.provider.personprovider/person")
The role of the main methods of the ContentProvider class:
Public Booleanoncreate ()
This method is invoked after ContentProvider is created, and when Android is powered on, ContentProvider is created the first time another application accesses it.
Public Uriinsert (URI Uri, contentvalues values)
This method is used for external applications to add data to ContentProvider.
public int Delete (URI Uri, String selection,string[] selectionargs)
This method is used for external applications to delete data from ContentProvider.
public int update (URI uri, contentvalues values, StringSelection, string[] selectionargs)
This method is used for external applications to update data in ContentProvider.
Public cursorquery (Uri Uri, String[]projection, string selection, string[] Selectionargs, string sortOrder)
This method is used for external applications to obtain data from ContentProvider.
Public String GetType (URI uri)
This method is used to return the MIME type of the data represented by the current URL. If the operation's data belongs to the collection type, then the MIME type string should start with vnd.android.cursor.dir/, for example: To get the URI of all person records to be content:// Cn.itcast.provider.personprovider/person, then the MIME type string returned should be: "Vnd.android.cursor.dir/person". If the data to be manipulated belongs to a vnd.android.cursor.item/type of data, then the MIME type string should start with the following example: Get a person record with ID 10, URI is content:// CN.ITCAST.PROVIDER.PERSONPROVIDER/PERSON/10, then the MIME type string returned should be: "Vnd.android.cursor.item/person".
public class Personcontentprovider Extendscontentprovider {
private Staticurimatcher Matcher = new Urimatcher (urimatcher.no_match);
private staticfinal int PERSONS = 1;
private staticfinal int person = 2;
Privatedbopenhelper Dbopenhelper;
static{
Matcher.adduri ("Cn.itcast.providers.personprovider", "person", PERSONS);
Matcher.adduri ("Cn.itcast.providers.personprovider", "person/#", person);
}
@Override
Public Booleanoncreate () {
Dbopenhelper =new Dbopenhelper (This.getcontext ());
return true;
}
@Override
public int Delete (URI Uri, String selection, string[] Selectionargs) {
Sqlitedatabasedb = Dbopenhelper.getwritabledatabase ();
int num = 0;//number of records deleted
Switch (Matcher.match (URI)) {
Case PERSONS:
Num =db.delete ("person", selection, Selectionargs);
Break
Case Person:
Long ID =contenturis.parseid (URI);
String where = "personid=" + ID;
if (selection!=null&&! "". Equals (selection)) {//personid=12 and name=?
where = where + "and" + selection;
}
Num =db.delete ("person", where, Selectionargs);
Break
Default
Throw newillegalargumentexception ("unkown uri:" + uri);
}
GetContext (). Getcontentresolver (). Notifychange (Uri,null);
return num;
}
@Override
Public String GetType (URI uri) {//Returns the data type of the current operation
Switch (Matcher.match (URI)) {
Case persons://operation is collection type data
return "Vnd.android.cursor.dir/person";
Case Person:
return "Vnd.android.cursor.item/person";
Default
Throw newillegalargumentexception ("unkown uri:" + uri);
}
}
@Override
Public URI insert (URI uri, contentvalues values) {
Sqlitedatabase db= dbopenhelper.getwritabledatabase ();
Long id = 0;
Switch (Matcher.match (URI)) {
Case PERSONS:
ID =db.insert ("person", "PersonID", values);//Get the ID of the record
GetContext (). Getcontentresolver (). Notifychange (Uri,null);
Returncontenturis.withappendedid (URI, id);//Returns the URI representing the new record
Case Person:
ID =db.insert ("person", "PersonID", values);//Get the ID of the record
String Struri =uri.tostring ();
Uri Personuri =uri.parse (struri.substring (0, Struri.lastindexof ("/")));
GetContext (). Getcontentresolver (). Notifychange (Personuri,null);
Returncontenturis.withappendedid (Personuri, id);
Default
Throw newillegalargumentexception ("unkown uri:" + uri);
}
}
@Override
Public Cursor query (URI uri, string[] projection, String selection, string[] selectionargs,string sortOrder) {
Sqlitedatabasedb = Dbopenhelper.getreadabledatabase ();
Switch (Matcher.match (URI)) {
Case PERSONS:
Returndb.query ("person", projection, selection, Selectionargs, NULL, null,sortorder);
Case Person:
Long ID =contenturis.parseid (URI);
String where = "personid=" + ID;
if (selection!=null&&! "". Equals (selection)) {//personid=12 and name=?
where = where + "and" + selection;
}
Returndb.query ("person", projection, where, Selectionargs, NULL, null,sortorder);
Default
Throw newillegalargumentexception ("unkown uri:" + uri);
}
}
@Override
Public intupdate (URI Uri, contentvalues values, String selection, string[] Selectionargs) {
Sqlitedatabasedb = Dbopenhelper.getwritabledatabase ();
int num = 0;//number of records modified
Switch (Matcher.match (URI)) {
Case PERSONS:
Num =db.update ("person", values, selection, Selectionargs);
Break
Case Person:
Long ID =contenturis.parseid (URI);
String where = "personid=" + ID;
if (selection!=null&&! "". Equals (selection)) {
where = where + "and" + selection;
}
Num =db.update ("person", values, where, Selectionargs);
Break
Default
Throw newillegalargumentexception ("unkown uri:" + uri);
}
GetContext (). Getcontentresolver (). Notifychange (uri,null);//Notification data changed
Returnnum;
}
}
Because the URI represents the data to be manipulated, we often need to parse the URI and fetch the data from the URI. The Android system provides two tool classes for manipulating URIs, Urimatcher and Contenturis respectively. Mastering the use of them will facilitate our development work.
The Urimatcher class is used to match the URI, and its usage is as follows:
First step, you need to match the URI path to all the registration, as follows:
Constant Urimatcher.no_match represents a return code that does not match any path
Urimatcher smatcher = new Urimatcher (urimatcher.no_match);
If the match () method matches the Content://cn.itcast.provider.personprovider/person path, the return match code is 1
Smatcher.adduri ("Cn.itcast.provider.personprovider", "person", 1);//Add need to match URI, if match will return match code
If the match () method matches the content://cn.itcast.provider.personprovider/person/230 path, the return match code is 2
Smatcher.adduri ("Cn.itcast.provider.personprovider", "person/#", 2);//#号为通配符数字, * denotes any character
Switch (Smatcher.match (Uri.parse ("CONTENT://CN.ITCAST.PROVIDER.PERSONPROVIDER/PERSON/10"))) {
Case 1
Break
Case 2
Break
DEFAULT://does not match
Break
}
After registering for a matching URI, you can use the Smatcher.match (URI) method to match the input URI, and if the match returns a matching code, the match code is the third argument passed in by the call to the Adduri () method, assuming that the match content:// Cn.itcast.provider.personprovider/person path, the return match code is 1
The Contenturis class is used to get the ID portion following the URI path, which has two more practical methods:
Withappendedid (URI, id) is used to add the ID portion to the path:
Uri uri = uri.parse ("Content://cn.itcast.provider.personprovider/person")
Uri Resulturi = Contenturis.withappendedid (URI, 10);
The URI after the build is: CONTENT://CN.ITCAST.PROVIDER.PERSONPROVIDER/PERSON/10
The Parseid (URI) method is used to get the ID part from the path:
URI URI