如何在 Marshmallow 上授予 android 应用 运行 权限?

How to grant permissions to android app running on Marshmallow?

我有一个应用程序可以创建两个文件夹并在这两个文件夹中存储数据 folders.When 我 运行 这个应用程序在低于 Marshmallow 的版本上一切正常 fine.But 当我 运行 Marshmallow 上的相同应用程序不会创建文件夹,除非我通过进入应用程序设置手动授予权限。 这是我用来检查权限的代码:-

private boolean checkWritePermission(){
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (result == PackageManager.PERMISSION_GRANTED){
            Log.d("WRITE_PERMISSION","Permission granted");

            return true;

        } else {

            return false;

        }
    }

    private boolean checkReadPermission(){
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
        if (result == PackageManager.PERMISSION_GRANTED){

            Log.d("READ_PERMISSION","Permission granted");
            return true;

        } else {

            return false;

        }
    }

这是我用来请求权限的代码:-

private void requestReadPermission()
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.READ_EXTERNAL_STORAGE)){

            Toast.makeText(context,"This permission allows you to read downloaded magazine",Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE_READ);
        }

    }

    private void requestWritePermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE)){

            Toast.makeText(context,"This permission allows you to download the magazine",Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE_WRITE);
        }
    }

    private void requestInternetPermission()
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.INTERNET)){

        }else{
            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.INTERNET},PERMISSION_REQUEST_CODE_INTERNET);
        }
    }

这是 onRequestPermissionsResult 的代码:-

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode)
    {
        case PERMISSION_REQUEST_CODE_READ:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {


            }else
            {

            }
            break;
        case PERMISSION_REQUEST_CODE_WRITE:
            if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {

            }else
            {

            }
            break;
        case PERMISSION_REQUEST_CODE_INTERNET:
            if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {

            }else
            {

            }
            break;




    }
}

然后我在 oncreate 中检查权限如下:-

 if (checkReadPermission() && checkWritePermission())
        {
           Log.d("ALL PERMISSIONS","All permissions granted");

        }else
        {
            requestInternetPermission();
            requestReadPermission();
            requestWritePermission();

        }

Github.

中有一些方便的权限检查库

我正在使用 PermissionDispatcher。看看吧。

https://github.com/hotchemi/PermissionsDispatcher

检查权限的代码有几个问题:

if (checkReadPermission() && checkWritePermission()) {
   Log.d("ALL PERMISSIONS","All permissions granted");
} else {
    requestInternetPermission();
    requestReadPermission();
    requestWritePermission();
}
  1. 您应该一次请求所有权限,而不是一个一个地请求
  2. 您首先检查的权限是 Manifest.permission.INTERNET。这是一个易受攻击的权限,因此总是被授予,这就是为什么你总能获得权限。
  3. 您只能请求 permission-group.STORAGE 的一项许可。即请求:Manifest.permission.READ_EXTERNAL_STORAGEManifest.permission.WRITE_EXTERNAL_STORAGE

尝试使用此代码:

if (checkWritePermission()) {
   Log.d("ALL PERMISSIONS","All permissions granted");
} else {
    requestWritePermission();
}