Andrews Dynamic Access Request

Andrews Dynamic Access Request

In Android development, we can write some authority in Manifest.xml documents to apply for permission, while there are some rights that we need to apply dynamic to the user when to use these privileges after Android 6.0.
Specific classification see here:
Android application for permission to apply for what needs to be dynamic

We now look at how to apply for the permission to be in the code.

   if(ContextCompat.checkSelfPermission(this, Manifest.permission.${PermissionnName})!=PackageManager.PERMISSION_GRANTED)
        {
            //判断用户是否拒绝过这个权限,提示用户手动去设置中给予我们的APP相关权限
            if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.${PermissionnName}))
            {

            }
            else
            {
                //申请这个权限
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSION_REQUEST_CODE);

            }
        }

Permissions name above code $ {PermissionnName} representatives want to apply, on-demand can be replaced

Then we also need to give permission to the user whether APP required to monitor, there is a callback function that can help us do this in Andrews in:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode)
        {
            case MY_PERMISSION_REQUEST_CODE:
            {
                if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    //用户允许了权限
                }
                else
                {
                    //用户拒绝了权限
                }
                break;
            }
        }
    }

This is the step of applying dynamic authority, with its own business logic can be.

Published 47 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41525021/article/details/104442352