运行时权限请求框架easypermissions
- 前言
之前使用过AndPermission权限申请库,当开发者执行有权限的代码发生异常时,AndPermission会抓到异常并回调到失败中,这里要注意的是会抓到任何异常,不仅仅是没有权限时的异常.
这使得我在正常授权的回调中的一些异常,甚至是崩溃的异常都被它捕捉了,因此导致了一个奇怪的现象:权限申请到了,但是弹出了我在onDenied()方法中的吐司...
那么,我当时是不知道是因为自己的逻辑出现了错误,导致这个现象的,所以到处去找解决办法,去翻AndPermission的Issues和反复阅读文档,遗憾的是当时我并没有主要到上面那句话...
于是,我就使用了另外一个权限申请库,这就是接下来要讲的easypermissions,它是googlesamples里的一个成员,因此是谷歌推荐的权限申请库,放心大胆的使用吧.
- easypermissions使用步骤
- 依赖
dependencies { implementation 'pub.devrel:easypermissions:1.2.0' }
请注意,EasyPermissions依赖于 Android Support Library
27.1.0
因此你需要使用compileSdkVersion 27
或更高版本。此更改应该是安全的,因为compileSdkVersion
不会更改应用行为。 - 必要准备
在你的Activity中重写
onRequestPermissionsResult
方法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); } }
- 申请权限
private fun checkPermissions() {
//首先判断权限是否已被授权,防止重复申请权限时,多次回调 if (EasyPermissions.hasPermissions(this@MainActivity, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO...)) { //do something } else { EasyPermissions.requestPermissions(this@MainActivity, "为了方便录音,我们需要您的麦克风权限和读写权限", 100, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO)
//第一个参数是context对象,第一个参数是提示字符串,第三个参数是请求码(可在回调中用来匹配),第四个参数是权限数组 } } - 回调
@AfterPermissionGranted(100) 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,"为了方便录音,我们需要您的麦克风权限和读写权限", 100, perms);
}
}此外,你还可以实现
PermissionCallbacks
接口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 // ... } }
值得注意的是onPermissionsGranted和onPermissionsDenied两回调方法并不是全授权或全拒绝时才会响应,只要有权限被用户授权,那么就会走onPermissionsGranted,对于onPermissionsDenied也是一样的
- 权限被拒绝时重复申请
在某些情况下,如果没有某些权限,您的应用将无法正常运行 如果用户使用“永不再问”选项拒绝这些权限,则您将无法向用户请求这些权限,并且必须在应用设置中更改这些权限。
EasyPermissions.somePermissionPermanentlyDenied(...)
在这种情况下,您可以使用该方法向用户显示对话框,并将其引导至您的应用程序的系统设置屏幕:@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(); } }
- 总结
文章开头提到由于使用AndPermission时遭遇了一种奇怪的现象,寻找解决办法而不得,才去了解easypermissions的,希望能够解决问题.替换运行时权限申请框架为easypermissions时,程序如愿崩溃了,我总算是看到了异常信息,解决掉之后,完美运行.
另外,我不打算换回AndPermission了,哈哈哈...