运行时权限:https://developer.android.google.cn/reference/android/Manifest.permission
用户一旦授权每个危险权限,同组其他权限也被授权。
7.3访问其他程序中的数据
使用ContentResolver类,可以通过Context中的getContentResolver()方法取得
CURD操作使用uri
标准URI格式:content://con.example.app.provider/table1 协议声明/autority(Package name+String)/path
查询:
Uri uri = Uri.parse("content://com.example.app.provider/table1");
Cursor cursor= getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder);
插入:
ContentValues values = new ContentValues();
values.put("column1","text");
getContentResolver().insert(uri,values);
更新:
ContentValues values = new ContentValues();
values.put("column1","text");
getContentResolver().updata(uri,values,"column2 = ?",new Stringp[]{"1"});
删除:
getContentResolver().delete(uri,"column2 = ?",new String[]{"1"});
7.4自定义内容提供器
UriMatcher 实现匹配内容URI
方法addURI():第一个参数,authority;第二个参数,path;第三个参数,自定义代码;
方法match():对传入的URI进行匹配,匹配成功则返回相应的自定义代码。
getType():用于获取Uri对象对应的MIME类型。
一个内容Uri对象对应的MIME:必须以vnd开头;如果以路径结尾,则后接android.cursor.dir/如果以id结尾,则后接android.cursor.item/;最后接vnd.<authority>.<path>
例子:content://com.example.app.provider/table1------vnd.android.cursor.dir/vnd.example.app.provider.table1
content://com.example.app.provider/table1/1-----vnd.android.cursor.item/vnd.example.app.provider.table1