内容提供者实现步骤
a.写一个类继承ContentProvider 重写onCreat query delete insert getType
b.在清单文件中声明对应的provider节点
authorities =通过这个字符串来决定访问的是哪个内容提供者,高版本的设备,还需要配置一个参数exported=true;
c.通过URLMatch添加URi的匹配规则
ContentResolver contentResolver=getContentResolver();
Uri uri=Uri.parse(“content://com.itheiasd.provider/query”);
Cursor cursor=contentResolver.query(uri,null,null,null,null);
private static final UriMathcher sURIMatcher=new UriMatcher(UriMather.NO_MATCH);
private static final int QURERY_SUCESS=0
private static final int INSERT_SUCESS=1
private static final int UPDATE_SUCESS=2
private static final int DELETE_SUCESS=3
static{
//给当前的url匹配器添加一个匹配规则
sURIMatcher.addURI(“com.itheiasd.provider”,“query”,QURERY_SUCESS);
sURIMatcher.addURI(“com.itheiasd.provider”,“insert”,INSERT_SUCESS);
sURIMatcher.addURI(“com.itheiasd.provider”,“update”,UPDATE_SUCESS);
sURIMatcher.addURI(“com.itheiasd.provider”,“delete”,DELETE_SUCESS);
}
URLIMatcher匹配上query的规则,就会返回QURERY_SUCESS
contentObserver 内容观察者
当数据发生改变的时候可以通过内容解析者发出一个通知,contentresolver.notifychanged
通过内容解析者还可以注册一个内容观察者
notifychanged(uri,null)
第一个参数uri notifychange之后通过这个uri来判断,该调用哪个内容观察者
第二个参数,内容观察者对象如果传null,注册了这个uri的所有观察者都能收到通知
发通知和注册内容观察者,都得先获取一个内容解析者
ContentResolver Resolver=getContentResolver();