这个写的太简介,不适合刚学的人看,等有空了详细整理一下。
1.先写一个类继承conentProvider.
2.注册。
<provider android:name="com.example.testcontacts.PersonCP" android:authorities="com.example.testcontacts.PersonCP" ></provider>
3.
content://com.example.testcontacts.PersonCP/insert
content://com.example.testcontacts.PersonCP/delete
content://com.example.testcontacts.PersonCP/update
content://com.example.testcontacts.PersonCP/query
4.创建匹配器
private static UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
5往匹配器里面添加匹配规则,就是访问的uri。
matcher.add("包名","insert",INSERT);
----
---
6.每一个方法里面先把传过来的uri。去匹配器里面查找一遍。
matcher.match(uri);
如何去访问contentProvider.
//类似中间人连接数据和访问人的桥梁。
(1)ConentResover resover=getContenteResolver();
//访问的uri
(2)Uri uri=Uri.parse("路径content://.................../...");就是反问的路径
(3)Cursor cursor=resover.query(uri,null,null,null,null);
while(cursor.moveToNext())
{
String name=cursor.getString(cursor.getColumnIndex("name"));
}
例如:
ContentResolver resolver =getContentResolver();
Uri uri=Uri.parse("");
ContentValues values=new ContentValues();
values.put(""."");
resovler.insert(uri,values);
//先得到resolver对象
//创建uri
//生成ContentValues对象。
//给values赋值。
//用resolver调用相应的方法。