ContentProvider和Resolver
程序员文章站
2022-03-10 18:25:20
...
- android Mainfest
//可根据不同应用设置不同权限
<permission android:name""
android:protectionLevel="选择"/>
<application
<provider
android:authorities=""
android:name=".MyContentProvider"
//可被调用
android:exported="true"
android:enabled="true"
//权限
android:readPermission=""
android:writePermission=""
android:grantUriPermissions="true"
>
</provider>
</application>
// 帮助类
//ContentUris可以帮助uri加入id
//provider类中
ContentUris.withAppendedId(uri,id);
//resolve类中
Uri uri2 = resolver.insert(uri,values);
long id=ContentUris.parseId(uri2);
增加
ContentProvider及内部方法
@Override
public Uri insert(Uri uri, ContentValues values) {
long id = 0;
if(values.size() > 0) {
id = db.insert("mingyang_tb", null, values);
}else{
String authority = uri.getAuthority();
String path = uri.getPath();
String query = uri.getQuery();
String name = uri.getQueryParameter("name");
String age = uri.getQueryParameter("age");
String gender = uri.getQueryParameter("gender");
Log.e("TAG","主机名:" + authority + ",路径:" + path + ",查询数据:" + query
+ ",姓名:" + name + ",年龄:" + age + ",性别:" + gender);
values.put("name" , name);
values.put("age" , age);
values.put("gender" , gender);
id = db.insert("info_tb",null,values);
}
//将id追加到uri后面
return ContentUris.withAppendedId(uri,id);
}
- resolve中处理insert
ContentResolver resolver=getContentResolver();
//寻找provider
Uri uri = Uri.parse("content://"authorities"");
ContentValues values = new ContentValues();
//put put put
Uri uri2 = resolver.insert(uri,values);
//方法返回(long)id
long id = ContentUris.parseId(uri2);
查询
- provider
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
//参数2:索要查询的列
//参数3:查询条件
//参数4:查询条件值
//参数5:分组
//参数6:分组条件
//参数7:排序
Cursor c = db.query("my_tb",projection,selection,selectionArgs,null,null,sortOrder);
return c;
}
- resolver
Cursor c = resolver.query(uri,null,null,null,null);
//参数2:每一个学员信息对象所显示的样式布局
//参数3:数据源
//参数4:查询结果中所有列的列名
//参数5:数据未来所要加载到的对应控件的id数组
//参数6:是否及时更新界面
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this ,
R.layout.item,
c,
new String[]{"_id","name","age","gender"},
new int[]{R.id.id_txt,R.id.name_txt,R.id.age_txt,R.id.gender_txt},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
stuList.setAdapter(adapter);
删除
返回值为修改的对象数量
URIMatcher
在provider中制定匹配规则 当调用匹配方法时 根据不同uri进行不同的处理
此处#表示所有数字都可以 *表示所有字符都可以
//在creat中制定
//参数:无法匹配
UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
//参数1: 当前provider的authorities
//参数2:子路径
//参数3:匹配成功返回的code
matcher.addURI("com.sjx.yic","hello/#",999);
matcher.addURI("com.sjx.yic","hello/*",998);
//在某个方法里
int code = matcher.match(uri);
在resolver中调用方法时传入不同的authorities就行
URI自带解析方法
- provider操作
if(values!>0){
String authority = uri.getAuthority();
//就是resolver中的?前面的字符串
String path = uri.getPath();
//整个?后面的所有
String query = uri.getQuery();
//单个获取?后面的参数根据域名
String name = uri.getQueryParameter("name");
String age = uri.getQueryParameter("age");
String gender = uri.getQueryParameter("gender");
values.put("name" ,name);
values.put("age" , age);
values.put("gender" , gender);
id = db.insert("my_tb",null,values);
}
resolver.insert(Uri.parse("content://com.imooc.myprovider/'任意字符串'?name=张三&age=23&gender=男"),
new ContentValues());
注意: oncreat在主线程中,其他4个方法都在子线程
推荐阅读