ContentProvider 详解

编程入门 行业动态 更新时间:2024-10-20 20:59:24

ContentProvider <a href=https://www.elefans.com/category/jswz/34/1770044.html style=详解"/>

ContentProvider 详解

一、定义 ContentProvider 主要用户不同进程之间的数据共享. 二、如何构建 1、首先建立继承与ContentProvider的子类并实现他 oncreate、query、getType、insert、delete、update方法 2、在manifest中生命<provider>节点 格式如下: <provider android:name="XX" android:authorites="xxx" android:permission="xxx" android:process=":xx" /> 其中android:permission 值的是 如果要使用 必须在manifest声明这个权限 3、建立数据访问类、用于contentprovider调用 4、建立UriMather  来匹配他们的uri 示例如下: static{ uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(AUTHORITY,PATH,1);
uriMatcher.addURI(AUTHORITY,PATH+ "/#",2);
uriMatcher.addURI(AUTHORITY,PATH+"/#/#", 3);
}
AUTHORITY 表示访问名称和manifest中的一致、
PATH表示他的子路径 具体用法见后面
5、实现各个方法oncreate、query、getType、insert、delete、update 其中oncreate 在主线程中所以必须轻量级,其他在子线程中;示例如下
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
int num = uriMatcher.match(uri);
if(num == 1){//对应uriMatcher.addURI(AUTHORITY,PATH,1);
count = db.update(EVENTS_TABLE, values, selection, selectionArgs);
}else if(num == 2){//对应uriMatcher.addURI(AUTHORITY,PATH+ "/#",2);
count = db.update(EVENTS_TABLE, values, ID + " = " + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
}else{
throw new IllegalArgumentException(
"Unknown URI " + uri);
}
    //通知他所以的监听者
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
三、如何使用
调用conext.getContentResolver().具体方法()【如:query、insert、delete、update】并返回cursor




更多推荐

ContentProvider 详解

本文发布于:2024-02-06 22:46:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1751417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详解   ContentProvider

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!