googlesamples之easypermissions使用

使用

1、 app/build.gradle

1
2
3
dependencies {
compile 'pub.devrel:easypermissions:0.3.0'
}

2、 在Activity / Fragment实现PermissionCallbacks,回调方法:

1
2
3
4
5
6
7
8
9
10
11
12
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// Some permissions have been granted
// ...
LogUtil.d("Some permissions have been granted=" + requestCode);
}
@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
// Some permissions have been denied
// ...
LogUtil.d("Some permissions have been denied=" + requestCode);
}

3、 在需要使用到权限之前调用methodRequiresTwoPermission方法,如相机和发送短信:

1
2
3
4
5
6
7
8
9
10
11
12
13
@AfterPermissionGranted(1010)
private void methodRequiresTwoPermission() {
String[] perms = {Manifest.permission.CAMERA, SEND_SMS};
if (EasyPermissions.hasPermissions(this, perms)) {
// Already have permission, do the thing
// ...
LogUtil.d("Already have permission, do the thing");
} else {
// Do not have permissions, request them now
LogUtil.d("Do not have permissions, request them now");
EasyPermissions.requestPermissions(this, "camera_and_send_sms", 1010, perms);
}
}

4、 还是要授权回调:

1
2
3
4
5
6
@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);
}

这样就over了。

源码地址

https://github.com/googlesamples/easypermissions

一点思考

就在《Android 6.0 RuntimePermission》文章里,我对Android 6.0 运行权限做了个简单封装,但没有考虑到fragment使用和勾上不再询问应该去setting,还一个问题,封装只是针对单个权限,推荐官方easypermissions库,来看看easypermissions是如何解决我未考虑的问题。

Fragment使用

直接提供了Fragment可以调用的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@SuppressLint("NewApi")
public static void requestPermissions(@NonNull Fragment fragment,
@NonNull String rationale,
@StringRes int positiveButton,
@StringRes int negativeButton,
int requestCode,
@NonNull String... perms) {
if (hasPermissions(fragment.getContext(), perms)) {
notifyAlreadyHasPermissions(fragment, requestCode, perms);
return;
}
if (shouldShowRationale(fragment, perms)) {
RationaleDialogFragmentCompat
.newInstance(positiveButton, negativeButton, rationale, requestCode, perms)
.show(fragment.getChildFragmentManager(), DIALOG_TAG);
} else {
fragment.requestPermissions(perms, requestCode);
}
}

不再询问去setting

我在《Android 6.0 RuntimePermission》说当勾上“不再询问”,只能选择拒绝,再次进入,shouldShowRequestPermissionRationale方法始终false,当onPermissionsDenied方法回调时,肯定是都是拒绝,再根据shouldShowRequestPermissionRationale方法不就知道是否勾上了“不再询问”,我咋没有想到这点呢:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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();
}
}

EasyPermissions的somePermissionPermanentlyDenied方法,就是根据shouldShowRequestPermissionRationale方法不就知道是否勾上了“不再询问”。

1
2
3
4
5
6
7
8
9
10
11
12
13
public static boolean somePermissionPermanentlyDenied(@NonNull Activity activity,
@NonNull List<String> deniedPermissions) {
for (String deniedPermission : deniedPermissions) {
if (permissionPermanentlyDenied(activity, deniedPermission)) {
return true;
}
}
return false;
}
public static boolean permissionPermanentlyDenied(@NonNull Activity activity,
@NonNull String deniedPermission) {
return !shouldShowRequestPermissionRationale(activity, deniedPermission);
}

跳转setting代码:

1
2
3
4
5
6
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", mContext.getPackageName(), null);
intent.setData(uri);
// Start for result
//noinspection NewApi The Builder constructor prevents this
startForResult(intent);

封装只针对单个权限

我应该封装多个权限,多权限是个数组,如果只有一个元素,不就是单个权限嘛!



联系作者

我的微信公众号:吴小龙同学,欢迎关注交流,公号回复关键字「1024」有惊喜哦。