Android permission dynamic application, after permanent ban, jump to the permission setting page to monitor the callback

During the use of certain permissions, the level of privacy involved in the user is relatively deep. In this case, adding permissions to the configuration file does not solve the problem. For example, when you call the library to select a photo, you cannot get the photo. Insufficient permissions are displayed. Also, for example, if the address is stored in the external storage instead of the software cache when using the camera to take a photo, then the photo cannot be displayed in the Imageview. Therefore, this kind of application needs to go further into the dynamic permission application.

1. Add permissions to the configuration file

insert image description here

   <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

insert image description here

android:requestLegacyExternalStorage="true"

2. Dynamic application

2.1 Define the permission list to be judged

//定义要判断的权限列表
    private String[] mPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

2.2 Define the method of permission checking

 //权限检查
    public void checkPermission() {
        //要判断的权限
        for (String p : mPermissions) {
            if (ContextCompat.checkSelfPermission(getApplicationContext(), p) == PackageManager.PERMISSION_DENIED) {
                //证明这个权限没有获得
                //发起权限请求
                ActivityCompat.requestPermissions(LoginActivity.this, mPermissions, 1);
            } else {
                Toast.makeText(getApplicationContext(), "您已经申请到了权限", Toast.LENGTH_SHORT).show();
            }
        }
    }

2.3 Rewrite the callback function of request processing

 @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 {
                    Toast.makeText(getApplicationContext(),"用户禁止了权限",Toast.LENGTH_SHORT).show();
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {//用户选择了禁止不再询问
                        final AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                        builder.setTitle("权限申请")
                                .setMessage("点击允许才可以使用我们的app哦")
                                .setPositiveButton("去允许", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface mDialog, int id) {
                                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                        Uri uri = Uri.fromParts("package", getPackageName(), null);//注意就是"package",不用改成自己的包名
                                        intent.setData(uri);
//                                        mDialog.dismiss();
                                        startActivityForResult(intent, 2);
                                    }
                                });
                        dialog=builder.show();
                        break;
                    }
                }
            }
        }
    }

2.4 Write the callback when the user returns to the setting page

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==2){
            dialog.dismiss();
            Toast.makeText(getApplicationContext(),"用户从设置页面返回"+(dialog==null),Toast.LENGTH_SHORT).show();
            checkPermission();//从设置页面回来 再重新检查是否拥有了权限
        }
    }

It should be noted that there are request codes when initiating a request and when jumping to the setting page, that is, corresponding processing can be performed according to the request code. For the above content, the permission code is 1, and the request code when setting the return is 2

Guess you like

Origin blog.csdn.net/qq_33183456/article/details/123697806