Processing of various statuses of permissions when Android6.0 is running (forbidden, no prompt/no question after prohibition)

There are many online tutorials about Android 6.0 runtime permissions, but most of them are not very comprehensive. It is a simple application, but is it really over? Does the logic flow work?

for example:

What if the user denies permission?

What if the permission is denied and you choose not to ask again?

What if the user chooses a permission and disables the rest?

What if the user does not choose to allow when going to the system to select permissions and then returns to our program?

Here's how to solve this problem: Let's take a picture first

 It's a little bit ugly, but you can see it.

Analyzing the problem, the user should give a pop-up box to tell the user that some functions cannot be used without authorization, and let the user authorize. If the user prohibits the permission and selects the option of not prompting again, then it will not be applied for permission. Prompt you to agree to the authorization. At this time, we will let the user jump to the system permission page to authorize. There is a problem in the implementation here. When the box is popped up twice, it should be two permissions, and it returned twice. Here, you just need to put the initialization of the bullet box outside, and only display the operation in the return of the permission. The following is the whole code for reference:

public class Main2Activity extends AppCompatActivity {
    private static final int NOT_NOTICE = 2;//如果勾选了不再询问
    private AlertDialog alertDialog;//禁止
    private AlertDialog mDialog;//禁止了并且不再提示
    //TODO 要申请的权限,这里我只放了读取文件和相机权限
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //禁止当前页面截屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        setContentView(R.layout.activity_main2);
        initDiaLogs();//初始化弹框
        requetPermission();//检测是否授权权限
    }

    /**
     * 检测是否有相关权限没有去授权
     * */
    private void requetPermission() {
        for (String permission : PERMISSIONS_STORAGE) {
            if (ContextCompat.checkSelfPermission(this, permission) != PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, 1);
            } else {
                Toast.makeText(this, "您已经申请了权限!", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 授权返回结果:
     * 1.始终允许
     * 2.禁止
     * 3.禁止后不再提示
     * */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PERMISSION_GRANTED) {//选择了“始终允许”
                    Toast.makeText(this, "" + "权限" + permissions[i] + "申请成功", Toast.LENGTH_SHORT).show();
                } else {
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {//用户选择了禁止不再询问
                        //TODO 不要在这里初始化Dialog,会导致弹框弹两遍
                        if (mDialog != null && mDialog.isShowing()) {
                            mDialog.show();
                        }
                    } else {//选择禁止
                        if (alertDialog != null && !alertDialog.isShowing()) {
                            alertDialog.show();
                        }
                    }
                }
            }
        }
    }

    /**
     * 初始化弹框
     * */
    public void initDiaLogs() {
        //禁止后不再提示的弹框
        AlertDialog.Builder builder1 = new AlertDialog.Builder(Main2Activity.this);
        builder1.setTitle("申请权限")
                .setMessage("由于您未授权会导致部分功能无法使用")
                .setPositiveButton("去授权", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (mDialog != null && mDialog.isShowing()) {
                            mDialog.dismiss();
                        }
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", getPackageName(), null);//注意就是"package",不用改成自己的包名
                        intent.setData(uri);
                        startActivityForResult(intent, NOT_NOTICE);
                    }
                });
        mDialog = builder1.create();
        mDialog.setCanceledOnTouchOutside(false);

        //禁止后的弹框
        AlertDialog.Builder builder = new AlertDialog.Builder(Main2Activity.this);
        builder.setTitle("申请权限")
                .setMessage("由于您未授权会导致部分功能无法使用")
                .setPositiveButton("去授权", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (alertDialog != null && alertDialog.isShowing()) {
                            alertDialog.dismiss();
                        }
                        requetPermission();
                    }
                });
        alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(false);
    }

    /**
     * 从系统权限那里过来如果没有权限需要再次申请
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == NOT_NOTICE) {
            requetPermission();//由于不知道是否选择了允许所以需要再次判断
        }
    }
}

The annotations are very complete. Here I put it directly in the class. You can encapsulate it in the tool class, or write it in the base class. It’s all right, just leave the permission interface

 

Guess you like

Origin blog.csdn.net/lanrenxiaowen/article/details/112615471