tips:访问 权限列表可以查看Android系统中完整的权限列表
在程序运行时申请权限的流程:
动态申请权限基本固定写法(代码示例):
//1.首先在Manifest文件中,申明需要的权限
//2.在OnCreate()方法中或者按钮的点击事件中:
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.权限名)!= PackageManager.PERMISSION_GR
//没有权限,进行动态申请 1为请求码,唯一值即可
ActivityCompat.requestPermissions(MainActivity.this,new String[]{ Manifest.permission.权限名},1);
}else{
//有权限,执行操作
}
//3.重写onRequestPermissionsResult():
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case 1:
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//有权限,执行操作
}else{
//没有权限
}
break;
default:
break;
}
}
通过Context中的getContentResolver()方法获取到实例
调用实例方法:都需要传入使用Uri.parse()方法解析的URI字符串,URI字符串标准写法:content://com.example.包名.provider/table名
query(uri,projection,selection,selectionArgs,sortOrder):返回的是一个Cursor对象
update(uri,values,“condition”,new String[]{}):
delete(uri,“condition”,new String[]{})
insert(uri,values):使用ContentValues
Content URI重要部分解析:(示例:content://com.example.provider/path/001)
内容提供器:
提供方:
<provider
android:name=".DatabaseProvider"
android:authorities="com.example.databasetest.provider"
android:enabled="true"
android:exported="true">
provider>
索要方:
<queries>
<package android:name="com.example.databasetest" />
queries>
创建自己的内容提供器:
方法 | 说明 |
---|---|
onCreate() | 用于初始化provider,完成数据库的创建和升级操作 |
query() | 返回数据给调用者,调用db.query()方法查询返回一个cursor对象 |
insert() | 插入新数据到ContentProvider,返回一个Uri |
update() | 更新ContentProvider中已经存在的数据,返回一个int即受影响的行数 |
delete() | 从Content Provider 中删除数据,返回一个int即被删除的行数 |
getType() | 返回Content provider数据的MIME类型 |
MIME字符串格式:
<provider
android:name="com.example..类名"
android:authorities="com.example..provider"
android:enabled="true"
android:exported="true">
provider>
public class DatabaseProvider extends ContentProvider {
//定义常量表示访问哪个表中的所有数据 以及哪个表中的单条数据
public static final int BOOK_DIR = 0;
public static final int BOOK_ITEM = 1;
public static final int CATEGORY_DIR = 2;
public static final int CATEGORY_ITEM = 3;
public static final String AUTHORITY = "com.example.databasetest.provider";
//匹配内容Uri
private static UriMatcher uriMatcher;
private MyDatabaseHelper dbHelper;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "book", BOOK_DIR);
uriMatcher.addURI(AUTHORITY, "book/#", BOOK_ITEM);
uriMatcher.addURI(AUTHORITY, "category", CATEGORY_DIR);
uriMatcher.addURI(AUTHORITY, "category/#", CATEGORY_ITEM);
}
public DatabaseProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int deleteRows = 0;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
deleteRows = db.delete("Book",selection,selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
deleteRows = db.delete("Book","id=?",new String[]{bookId});
break;
case CATEGORY_DIR:
deleteRows = db.delete("Category",selection,selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
deleteRows = db.delete("Category","id=?",new String[]{categoryId});
break;
default:
break;
}
return deleteRows;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.book";
case CATEGORY_DIR:
return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.category";
case CATEGORY_ITEM:
return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.category";
default:
break;
}
return null;
}
//添加数据
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Uri uriReturn = null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
case BOOK_ITEM:
long newBookId = db.insert("Book",null,values);
uriReturn = uri.parse("content://"+AUTHORITY+"/book/"+newBookId);
break;
case CATEGORY_DIR:
case CATEGORY_ITEM:
long newCategoryId = db.insert("Category",null,values);
uriReturn = uri.parse("content://"+AUTHORITY+"/category/"+newCategoryId);
break;
default:
break;
}
return uriReturn;
}
@Override
public boolean onCreate() {
dbHelper = new MyDatabaseHelper(getContext(),"BookStore.db",null,2);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
cursor = db.query("Book",projection,selection,selectionArgs,null,null,sortOrder);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
cursor = db.query("Book",projection,"id=?",new String[]{bookId},null,null,sortOrder);
break;
case CATEGORY_DIR:
cursor = db.query("Category",projection,selection,selectionArgs,null,null,sortOrder);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
cursor = db.query("Category",projection,"id=?",new String[]{categoryId},null,null,sortOrder);
break;
default:
break;
}
return cursor;
}
//更新数据
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int updateRows = 0;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
updateRows = db.update("Book",values,selection,selectionArgs);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
updateRows = db.update("Book",values,"id=?",new String[]{bookId});
break;
case CATEGORY_DIR:
updateRows = db.update("Category",values,selection,selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
updateRows = db.update("Category",values,"id=?",new String[]{categoryId});
break;
default:
break;
}
return updateRows;
}
}
以上类中使用到的方法解析:
URI的格式:
以通配符的方式来匹配:
借助UriMatcher类来匹配内容Uri:uriMatcher.addURI()接收三个参数,返回值为能够匹配Uri对象所对应的自定义代码:
Uri对象的getPathSegments()方法:会将内容URI权限之后的部分以"/"进行分隔,得到一个字符串列表
第0个位置存放的是路径,第1个位置存放的是id
//在方法中或按钮点击事件中
//确定好要访问的uri
uri = Uri.parse("content://com.example.databasetest.provider/book");
//TODO: 组装数据 ContenValues
//调用一系列方法进行CRUD
getContentResolver().insert()
getContentResolver().update()
getContentResolver().delete()
getContentResolver().query()