android EasyPermissions 使用教程

github:https://github.com/anymyna/ui

步骤:

1、 Module build.gradle 添加EasyPermissions,manifest增加相关权限

implementation 'pub.devrel:easypermissions:2.0.1'







2、Activity (or Fragment) 重写 onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    Log.d(TAG, "onRequestPermissionsResult");
    // EasyPermissions handles the request result.
    EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

3、权限申请
AfterPermissionGranted注解是可选的,如果有该注解的话,那么当request值对应的权限申请通过的话会自动调用该方法。

@AfterPermissionGranted(RC_CAMERA_PERM)
public void cameraTask() {
    if (hasCameraPermission()) {
        // Have permission, do the thing!
        Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show();
    } else {
        // Ask for one permissiononRequestPermissionsResult
        Log.d(TAG, "cameraTask requestPermissions:" );
        EasyPermissions.requestPermissions(
                this,
                getString(R.string.rationale_camera),
                RC_CAMERA_PERM,
                Manifest.permission.CAMERA);
    }
}

4、实现 EasyPermissions.RationaleCallbacks 自定义交互用户点击接受和拒绝按钮.

@Override //   权限请求dialog,用户点击接受
public void onRationaleAccepted(int requestCode) {
    Log.d(TAG, "onRationaleAccepted:" + requestCode);
}

@Override //权限请求dialog,用户拒绝按钮
public void onRationaleDenied(int requestCode) {
    Log.d(TAG, "onRationaleDenied:" + requestCode);
}

5、实现 PermissionCallbacks 接口 处理权限授权和拒绝

@Override
public void onPermissionsGranted(int requestCode, @NonNull List perms) {
    Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size());
}

@Override
public void onPermissionsDenied(int requestCode, @NonNull List 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();
    }
}

6、系统设置app界面返回处理

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
        String yes = getString(R.string.yes);
        String no = getString(R.string.no);
        //系统设置app界面 返回
        // Do something after user returned from app settings screen, like showing a Toast.
        Toast.makeText(
                this,
                getString(R.string.returned_from_app_settings_to_activity,
                        hasCameraPermission() ? yes : no,
                        hasLocationAndContactsPermissions() ? yes : no,
                        hasSmsPermission() ? yes : no),
                Toast.LENGTH_LONG)
                .show();
    }
}

7、管理权限相关命令

按组查看权限和状态:adb shell pm list permissions -d -g

同意和拒绝一个或者多个权限:adb shell pm [grant | revoke] …

打开READ_CONTACTS权限

adb shell pm grant com.name.app android.permission.READ_CONTACTS

关闭READ_CONTACTS权限

adb shell pm revoke com.name.app android.permission.READ_CONTACTS

参考:https://github.com/googlesamples/easypermissions

你可能感兴趣的:(Android)