AndPermission 运行时权限管理使用

AndPermission是一个运行权限管理库,兼容Android O,同时最大程度上兼容了国产机。

仓库地址: https://github.com/yanzhenjie/AndPermission

1.在model 的build.gradle中添加依赖

dependencies {
    api fileTree(include: ['*.jar'], dir: 'libs')
    api project(':baselibrary')
    ...
    //权限检测
    implementation 'com.yanzhenjie:permission:2.0.0-rc12'
    //弹出对话框
    implementation 'com.yanzhenjie.alertdialog:alertdialog:1.0.1'
}

 

2.在Manifest.xml 中注册需要的权限:














3..在闪屏界面或者调用该权限的界面添加申请权限的代码:

public void onCreate(){

... 

new Handler().postDelayed(this::requestFilePermission, 3000); //先显示闪屏界面,3秒后调用权限检测

}

private void requestFilePermission() {
    requestPermission(this, Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_PHONE_STATE, Permission.CAMERA);

}

 

 

public void requestPermission(Activity activity, String... permissions) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //Build.VERSION_CODES.M
        AndPermission.with(activity)
                .runtime()
                .permission(permissions)
                .rationale(new Rationale>() {
                    @Override
                    public void showRationale(Context context, List data, RequestExecutor executor) {

                        List permissionNames = Permission.transformText(context, data);
                        String message = context.getString(com.hjq.baselibrary.R.string.message_permission_rationale, TextUtils.join("\n", permissionNames));

                        AlertDialog.newBuilder(context)
                                .setCancelable(false)
                                .setTitle(com.hjq.baselibrary.R.string.title_dialog)
                                .setMessage(message)
                                .setPositiveButton(com.hjq.baselibrary.R.string.resume, new OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        executor.execute();
                                    }
                                })
                                .setNegativeButton(com.hjq.baselibrary.R.string.cancel, new OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        executor.cancel();
                                    }
                                })
                                .show();


                    }
                })
                .onGranted(data -> {
                    goHomeActivity();
                })
                .onDenied(data -> {
                    if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {


                        AndPermission.with(activity)
                                .runtime()
                                .setting()
                                .onComeback(new Action() {
                                    @Override
                                    public void onAction() {
                                        if (AndPermission.hasPermissions(activity, permissions)) {

                                            goHomeActivity();
                                        }
                                    }
                                })
                                .start();


                    }

                })
                .start();
    }

}

 

 

private void goHomeActivity() {

    startActivity(LoginActivity.class);
    finish();
}

 

你可能感兴趣的:(Android基础)