2020-11-12(内容提供者,内容解析者,内容观察者)
內(nèi)容提供者實(shí)現(xiàn)步驟
a.寫一個(gè)類繼承ContentProvider 重寫onCreat query delete insert getType
b.在清單文件中聲明對(duì)應(yīng)的provider節(jié)點(diǎn)
authorities =通過這個(gè)字符串來決定訪問的是哪個(gè)內(nèi)容提供者,高版本的設(shè)備,還需要配置一個(gè)參數(shù)exported=true;
c.通過URLMatch添加URi的匹配規(guī)則
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{
//給當(dāng)前的url匹配器添加一個(gè)匹配規(guī)則
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的規(guī)則,就會(huì)返回QURERY_SUCESS
contentObserver 內(nèi)容觀察者
當(dāng)數(shù)據(jù)發(fā)生改變的時(shí)候可以通過內(nèi)容解析者發(fā)出一個(gè)通知,contentresolver.notifychanged
通過內(nèi)容解析者還可以注冊(cè)一個(gè)內(nèi)容觀察者
notifychanged(uri,null)
第一個(gè)參數(shù)uri notifychange之后通過這個(gè)uri來判斷,該調(diào)用哪個(gè)內(nèi)容觀察者
第二個(gè)參數(shù),內(nèi)容觀察者對(duì)象如果傳null,注冊(cè)了這個(gè)uri的所有觀察者都能收到通知
發(fā)通知和注冊(cè)內(nèi)容觀察者,都得先獲取一個(gè)內(nèi)容解析者
ContentResolver Resolver=getContentResolver();
總結(jié)
以上是生活随笔為你收集整理的2020-11-12(内容提供者,内容解析者,内容观察者)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020-11-12(JNI开发常见错误
- 下一篇: 2020-11-13(c++下JNI开发