Android 动态权限申请框架easypermissions使用详解
前言
Android M(6.0)开始,一些危险的权限需要动态申请,称为动态权限或运行时权限,比如摄像头的权限,存储读取权限等。
easypermissions是一个为了简化运行权限申请过程的框架,封装了原生API的一些代码
依赖
注意AndroidX和使用support包引用的是不同的依赖
dependencies {
// For developers using AndroidX in their applications
implementation 'pub.devrel:easypermissions:3.0.0'
// For developers using the Android Support Library
implementation 'pub.devrel:easypermissions:2.0.1'
}
回调转发
重写Activity或者Fragment的onRequestPermissionsResult方法,在里面调用EasyPermissions.onRequestPermissionsResult()方法将申请权限结果的回调交由easypermissions处理
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
}
申请权限
如下methodRequiresTwoPermission()方法需要用到摄像头和定位权限,
- 首先调用EasyPermissions.hasPermissions()判断所需全选是否已获取,若已获取,直接执行业务代码
- 若所需权限未获取到,调用EasyPermissions.requestPermissions()方法申请
- @AfterPermissionGranted标识methodRequiresTwoPermission()方法,在成功获取所需权限后会再次回调该方法,此时代码会走到处理业务的相关代码
public static final int RC_CAMERA_AND_LOCATION = 1; // requestCode
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
private void methodRequiresTwoPermission() {
String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
if (EasyPermissions.hasPermissions(this, perms)) {
// Already have permission, do the thing
// ...
} else {
// Do not have permissions, request them now
EasyPermissions.requestPermissions(this, getString(R.string.camera_and_location_rationale),
RC_CAMERA_AND_LOCATION, perms);
}
}
自定义提示对话框
动态申请权限的时候首先会先弹出一个对话框,在该对话框内可以说明为何应用需要申请这些权限,通过EasyPermissions.requestPermissions()动态申请权限弹出的对话框是默认的,我们也可以调用如下代码自定义对话框的确定/取消按钮内容及对话框的样式
EasyPermissions.requestPermissions(
new PermissionRequest.Builder(this, RC_CAMERA_AND_LOCATION, perms)
.setRationale(R.string.camera_and_location_rationale)
.setPositiveButtonText(R.string.rationale_ask_ok)
.setNegativeButtonText(R.string.rationale_ask_cancel)
.setTheme(R.style.my_fancy_style)
.build());
回调
在调用EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)申请权限时,最后一个参数就是回调,类型为EasyPermissions.PermissionCallbacks
由于上面我们使用了@AfterPermissionGranted注解来处理获取所有所需权限的回调,简化了处理流程,当然这是可选的,我们也可以使用EasyPermissions.PermissionCallbacks来处理回调,需实现里面的onPermissionsGranted()和onPermissionsDenied()方法
当申请的权限里有一个或多个被允许时,会回调onPermissionsGranted()方法
当申请的权限里有一个或多个被拒绝时或者有的权限之前已被拒绝且用户选择"不再提示"时,会回调onPermissionsDenied()方法
运行时权限一次可以同时申请多个,用户可以自行选择允许哪一些、拒绝哪一些权限,所以可能出现用户允许某些权限同时又拒绝另外一些权限,这时onPermissionsGranted()和onPermissionsDenied()方法同时都会被回调,可以在里面做一些相关处理
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// Some permissions have been granted
// ...
}
@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
// Some permissions have been denied
// ...
}
}
跳转设置页面
申请权限的时候用户可能拒绝且选择"不再提示",这种情况就无法再通过运行时申请权限,只能用户到设置页面主动允许该权限。
有些权限被拒绝后程序的某些功能无法正常运行,若权限被用户拒绝且选择选择"不再提示"后,我们可以通过引导用户到设置页面开启
1.首先调用EasyPermissions.somePermissionPermanentlyDenied()确认是否有权限被用于拒绝且选择"不再提示"永久拒绝
2.若有权限被永久拒绝,调用new AppSettingsDialog.Builder(this).build().show()弹出一个对话框引导用户到设置页面开启
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
// (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
// This will display a dialog directing them to enable the permission in app settings.
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this).build().show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
// Do something after user returned from app settings screen, like showing a Toast.
Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)
.show();
}
}
RationaleCallbacks
rational对话框就是我们动态请求权限时先弹出的对话框,说明为何要申请这些权限。我们可以通过设置EasyPermissions.RationaleCallbacks回调来处理用户同意或者拒绝请求的回调
官方文档只是说明可以设置但是却没说明如何设置,也没找到相关设置的方法,后来直接查看源码才知道,只需将请求动态权限的Activity或者Fragment实现EasyPermissions.RationaleCallbacks接口即可,在RationaleDialogFragment内部会判断Activity或者Fragment是否实现EasyPermissions.RationaleCallbacks接口,有的话会强制转化,直接看以下代码就明白了
RationaleDialogFragment就是弹出的rational询问对话框
public void onAttach(Context context) {
super.onAttach(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && getParentFragment() != null) {
if (getParentFragment() instanceof EasyPermissions.PermissionCallbacks) {
mPermissionCallbacks = (EasyPermissions.PermissionCallbacks) getParentFragment();
}
if (getParentFragment() instanceof EasyPermissions.RationaleCallbacks){
mRationaleCallbacks = (EasyPermissions.RationaleCallbacks) getParentFragment();
}
}
if (context instanceof EasyPermissions.PermissionCallbacks) {
mPermissionCallbacks = (EasyPermissions.PermissionCallbacks) context;
}
if (context instanceof EasyPermissions.RationaleCallbacks) {
mRationaleCallbacks = (EasyPermissions.RationaleCallbacks) context;
}
}
实现EasyPermissions.RationaleCallbacks接口
public class MainActivity extends AppCompatActivity implements EasyPermissions.RationaleCallbacks {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onRationaleAccepted(int requestCode) {
// Rationale accepted to request some permissions
// 好伤心,被她拒绝了
}
@Override
public void onRationaleDenied(int requestCode) {
// Rationale denied to request some permissions
// 好开心啊,她答应了
}
}
参考