2020-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();
總結
以上是生活随笔為你收集整理的2020-11-12(内容提供者,内容解析者,内容观察者)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020-11-12(JNI开发常见错误
- 下一篇: 2020-11-13(四大组件简单回忆内