600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > -11-12(内容提供者 内容解析者 内容观察者)

-11-12(内容提供者 内容解析者 内容观察者)

时间:2023-02-02 14:16:29

相关推荐

-11-12(内容提供者 内容解析者 内容观察者)

内容提供者实现步骤

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();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
相关阅读
内容提供者2

内容提供者2

2021-09-07

创建内容提供者

创建内容提供者

2021-10-31

内容提供者的概念

内容提供者的概念

2024-01-30

ContentProvider内容提供者

ContentProvider内容提供者

2018-06-16