Content Provider: A component that must be placed under the application's main package or the child package of the application;
The configuration of the component needs to be configured in the manifest file, and the content provider needs to be configured in the application node;
The role of the content provider in the application is to share data externally ( any type of data ), and other programs can crud the data, such as the Address Book;
If the file is used to share data externally, because of the different types of files and need to use different API access methods to cause access complexity, and content providers provide a unified API to operate the data;
<provider
Android:name= ". Personprovider <!--The name of the content provider class-->
Android:authorities= "Cn.wordtech.providers.personprovider"
Android:exported= "false" ><!--address Android Permission denial error! , you need to configure this when listening for content provider data changes-->
</provider>
Other:
Android:authorities: Specifies a unique identity for the content provider so that the application can only obtain this provider;
The Uri represents the data to be manipulated;
A URI contains two main pieces of information: Thecontentprovider that 1>> needs to operate, and2>> what data is in ContentProvider
ContentProvider Scheme for (content provider) is already defined by Android,scheme: content://
The host name (or authority) is used to uniquely identify this contentprovider, and the external caller can find it based on this identity.
Pathcan be used to represent the data we want to manipulate, and the path is built according to the business.
Ex
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, you can build such a path:/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
The data you want to manipulate is not necessarily a file in a database, it can be a file, XML, or a network, and other ways
Ex
To manipulate the name node under the person node in the XML file, you can build such a path:/person/name
Copy Code code as follows:
public class Personprovider extends ContentProvider {//Content provider need to inherit from ContentProvider class
In the censored, there are two kinds of situations:
Person to manipulate the entire table
PERSON/ID to the ID corresponding record in the table
Private Dbopenhelper Dbopenhelper;
private static final Urimatcher MATCHER = new Urimatcher (urimatcher.no_match);//New Urimatcher (code); Code is the value returned when the match is unsuccessful;
private static final int PERSONS = 1;
private static final int person = 2;
Set up matches
static {
Matcher.adduri ("Cn.wordtech.providers.personprovider", "person", PERSONS);
Matcher.adduri ("Cn.wordtech.providers.personprovider", "person/#", person);/#号表示数字
}
Content://cn.wordtech.providers.personprovider/person
@Override
public Boolean onCreate () {
Called by the system, when an instance of ContentProvider is created, it is invoked, and when Android is powered on, the ContentProvider is created when the first time an application accesses ContentProvider.
Dbopenhelper = new Dbopenhelper (GetContext (), 1);
return false;
}
//can be used to query data for external applications, returning the cursor object from the query
@Override
public Cursor query (Uri uri, string[] projection, String selection,
string[] Selectionargs, string sortOrder) {
sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
switch (Matcher.match (URI)) {
case 1:
return db.query ("person" , projection, selection, Selectionargs,
null, NULL, sortOrder);
case 2:
long rowid = Contenturis.parseid (URI);//return ID
to manipulate string where = "personid=" + rowid;
if (selection!= null &&! "". Equals (Selection.trim ())) {
where + = "and" + selection;
&NBSP;&NBSP;&NBSP}
return db.query ("person", projection, where, Selectionargs, NULL,
null, SortOrder);
Default
throw new IllegalArgumentException ("");
}
}
This method returns the MIME type of the data represented by the current URI.
If the operation's data belongs to the collection type, the MIME string begins with "Vnd.android.cursor.dir"
If the operation's data is of a non-collection type, the MIME string begins with "Vnd.android.cursor.item"
@Override
Public String GetType (Uri uri) {
Switch (Matcher.match (URI)) {
Case 1:
return "Vnd.android.cursor.dir/person";
Case 2:
return "Vnd.android.cursor.item/person";
Default
throw new IllegalArgumentException ("");
}
}
This method needs to return the URI corresponding to the action record
@Override
Public URI insert (URI uri, contentvalues values) {
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
Switch (Matcher.match (URI)) {
Case 1:
Long rowID = Db.insert ("Person", "", values);//return line number? PRIMARY key value
Uri Inserturi = URI
. Parse ("content://com.sqlite.personprovider/person/"
+ rowID);
Uri Inserturi = Contenturis.withappendedid (URI, ROWID);
return Inserturi;
Default
throw new IllegalArgumentException ("This is unknow URI:" + uri);
}
}
//returns the number of rows affected
@Override
public int Delete (URI Uri, String selection, string[] Selectionargs) {
sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
int num = 0;
switch (Matcher.match (URI)) {
case 1:
num = db.delete ("Person", Selection, Selectionargs);/clear the entire table
break;
case 2:
long rowid = Contenturis.parseid (URI);//return ID to be manipulated
String where = "personid=" + rowid;
if (selection!= null &&! "". Equals (Selection.trim ())) {
where + = "and" + selection;
&NBSP;&NBSP;&NBSP}
num = Db.delete ("Person", where, Selectionargs);
break;
default:
throw new IllegalArgumentException ("");
&NBSP;&NBSP}
return num;
.}
@Override//Returns the number of rows affected
public int update (URI uri, contentvalues values, String selection,string[] Selectionargs) {
sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
int num = 0;
switch (Matcher.match (URI)) {
case 1:
num = db.update ("Person", Values, selection, Selectionargs);
break;
case 2:
long rowid = Contenturis.parseid (URI);//return ID
to manipulate string where = "personid=" + rowid;
if (selection!= null &&! "". Equals (Selection.trim ())) {
where + = "and" + selection;
&NBSP;&NBSP;&NBSP}
num = db.update ("Person", values, where, Selectionargs);
break;
default:
throw new IllegalArgumentException ("");
&NBSP;&NBSP}
return num;
&NBSP}
}
The following is a test of the previous class
Copy Code code as follows:
public class Accesscontentprovidertest extends Androidtestcase {
public void Testinsert () {
Uri uri = uri.parse ("Content://cn.wordtech.providers.personprovider/person")//content provider based on identity name
Contentresolver CR = This.getcontext (). Getcontentresolver (); This class provides applications access to the content model
Contentvalues values = new Contentvalues ();
Values.put ("name", "Livingstone");
Values.put ("Phone", "110");
Values.put ("Amount", "1111111111");
Cr.insert (URI, values);//The value of the content provider is invoked within the CR;
}
public void Testdelete () {
Uri uri = uri.parse ("CONTENT://CN.WORDTECH.PROVIDERS.PERSONPROVIDER/PERSON/1")//content provider based on identity name
Contentresolver CR = This.getcontext (). Getcontentresolver ();
Cr.delete (URI, NULL, NULL);
}
public void Testupdate () {
Uri uri = uri.parse ("CONTENT://CN.WORDTECH.PROVIDERS.PERSONPROVIDER/PERSON/2")//content provider based on identity name
Contentresolver CR = This.getcontext (). Getcontentresolver ();
Contentvalues values = new Contentvalues ();
Values.put ("name", "Livingstone11");
Cr.update (URI, values, NULL, NULL);
}
public void Testquery () {
Uri uri = uri.parse ("Content://cn.wordtech.providers.personprovider/person")//content provider based on identity name
Contentresolver CR = This.getcontext (). Getcontentresolver ();
Cursor Cursor = cr.query (URI, NULL, NULL, NULL, "PersonID ASC");
while (Cursor.movetonext ()) {
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
LOG.I ("name", name);
}
}
}