# #内容提供者笔记 # #
# # #步骤 # #
1. Create a method that implements the subclass Mycontentprovider of the ContentProvider and overrides the parent class
2, as one of the four components of Android, to register the provider tag in the Manifest.xml file
<provider
Android:name= "Cn.itcast.db.MyContentProvider"
android:authorities= "Cn.itcast.db.persondb" >
</provider>
> which
>**name** is the file path where the MyProvider resides;
>**authorities** is custom content, but preferably as the name implies (should be the package name/database name where the database resides)
3, create Urimatcher object, parameter comfort to **-1**
public static Urimatcher Urimacher = new Urimatche (-1);
4. Increase the URI path in the static code block
static{
Urimacher.adduri ("cn.itcast.db.persondb", "Query", 1);
Urimacher.adduri ("Cn.itcast.db.persondb", "Insert", 2);
Urimacher.adduri ("Cn.itcast.db.persondb", "delete", 3);
Urimacher.adduri ("Cn.itcast.db.persondb", "Update", 4);
Content://cn.itcast.db.persondb/insert
Content://cn.itcast.db.persondb/delete
Content://cn.itcast.db.persondb/update
Content://cn.itcast.db.persondb/query
}
5. When overriding the Query method
Public Cursor query (URI uri, string[] projection, String selection,
String[] Selectionargs, String sortOrder) {
TODO auto-generated Method Stub
int result = Urimacher.match (URI);
if (result==1) {
Sqlitedatabase db = Sqlite.getreadabledatabase ();
return Db.query ("info", projection, selection, Selectionargs, NULL, NULL, sortOrder);
}else{
throw new RuntimeException ("Path error, request data failed");
}
}
6. Get content from content providers in other applications
Uri uri = uri.parse ("Content://cn.itcast.db.persondb/query");
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 phone = cursor.getstring (Cursor.getcolumnindex ("Phone"));
System.out.println (id+ "--" +name+ "--" +phone ");
}
Cursor.close ();
* Used with URI paths added by the content provider *
**//content://cn.itcast.db.persondb/insert
Content://cn.itcast.db.persondb/delete
Content://cn.itcast.db.persondb/update
content://cn.itcast.db.persondb/query**
Content Provider Learning Notes