内容提供器
(Content Provider)
主要用于在不同程序之间实现数据共享的功能,提供了一套完整的当前程序访问另一程序的机制,同时保证数据的安全性
运行时权限
Android目前将权限按照安全等级分为两类,一种是普通权限,在运行过程中,由于这类权限往往不涉及用户隐私,系统会自动帮我们授权。另一种是危险权限,涉及到用户的隐私与个人信息,只有用户授权同意才可以处理
在Android6.0之前,所有权限的获取都是在软件安装前进行申请,并且不接受权限意味着无法正常安装APP,在Android6.0之后,引入了运行时权限的概念,用户拒绝某些权限或暂时不处理,可以正常安装APP,等需要使用这些权限时再向用户提交申请
每添加一个权限,都需要在AndroidMainfest.xml中提前声明
在程序运行时申请权限
package cn.ywrby.runtimepermissiontext;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_call = findViewById(R.id.bt_call);
bt_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//没有获取权限的情况下,会先申请权限(requestPermissions弹出权限申请框)
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
}
//获得权限的前提下,直接执行call方法,不会再次申请
else {
call();
}
}
});
}
//定义拨打电话的方法
public void call() {
try {
//隐式Intent,指定的活动ACTION_CALL是系统内置的打电话动作
//注意,这和之前的打开拨号界面不同,打开拨号界面是普通权限,直接拨号是危险权限,需要声明并得到用户同意
//这里为了防止程序崩溃,采用了异常捕获的格式
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
} catch (SecurityException e) {
e.printStackTrace();
}
}
//在调用requestPermission请求权限并弹出权限申请对话框后回调
//授权的结果封装在grantResults中,这里通过判断申请结果执行对应操作
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
//在申请获得许可的情况下执行危险权限的操作
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
call();
} else {
Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
}
访问其他程序数据
ContentResolver的基本用法
用于访问其他应用程序内容提供器中的共享数据,可以通过Context类中的getContentResolver获取到相应的实例
ContentResolver中提供了一系列方法对数据进行CRUD操作,例如insert,delete,update,query等等,操作基本与SQLiteDatabase一致。
不同在于ContentResolver是不接受表名参数的,取而代之的是Uri参数,用于指明表的相关路径,避免单独的表名导致无法查询到相关表的信息
Uri组成
content://程序的包名/表名
例如:
content://cn.ywrby.provider/table1
ContentResolver经过查询后的返回值类型也是Cursor,只需要利用移动游标再遍历就可以获取我们需要的数据内容
利用ContentResolver读取系统联系人
package cn.ywrby.contactstest;
import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//声明适配器
ArrayAdapter<String> adapter;
//用来存储获取到的相关数据
List<String> contactsList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView contactsView=findViewById(R.id.lv_contacts);
//绑定适配器
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactsList);
//传入适配器
contactsView.setAdapter(adapter);
//检查权限状态
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
}else {
readContacts();
}
}
//读取系统联系人
public void readContacts(){
Cursor cursor=null;
try {
//利用查询语句获取全部联系人信息
cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
//循环遍历得到的信息,并存储到数据列表中
if(cursor!=null){
while(cursor.moveToNext()){
String displayName=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactsList.add(displayName+"\n"+number);
}
//刷新ListView
adapter.notifyDataSetChanged();
}
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭Cursor
if(cursor!=null){
cursor.close();
}
}
}
//请求权限的结果并作出处理
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
readContacts();
}else{
Toast.makeText(this,"You denied the permission!",Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
}
实现一个完整的内容提供器
package cn.ywrby.databasetext;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import java.sql.Struct;
public class DatabaseProvider extends ContentProvider {
public static final int BOOK_DIR=0; //访问book表中所有数据
public static final int BOOK_ITEM=1; //访问book表中单条数据
public static final int CATEGORY_DIR=2; //访问category表中所有数据
public static final int CATEGORY_ITEM=3; //访问category表中单条数据
//路径参数
public static final String AUTHORITY="cn.ywrby.databasetext.provider";
//利用UriMatcher可以轻松实现匹配内容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 boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
dbHelper=new MyDatabaseHelper(getContext(),"BookStore.db",null,2);
return true;
}
//定义删除方法
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
SQLiteDatabase db=dbHelper.getReadableDatabase();
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);
//getPathSegments将内容Uri权限之后的部分按“\”拆开
//并且将结果储存到字符串列表中,所以第0项就是内容Uri,第1项就是ID
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 Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
SQLiteDatabase db=dbHelper.getReadableDatabase();
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 Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
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) {
// TODO: Implement this to handle requests to update one or more rows.
SQLiteDatabase db=dbHelper.getReadableDatabase();
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;
}
//获取MIME类型
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
switch (uriMatcher.match(uri)){
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.cn.ywrby.databasetext.provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.cn.ywrby.databasetext.provider.book";
case CATEGORY_DIR:
return "vnd.android.cursor.dir/vnd.cn.ywrby.databasetext.provider.category";
case CATEGORY_ITEM:
return "vnd.android.cursor.item/vnd.cn.ywrby.databasetext.provider.category";
}
return null;
}
}