简体   繁体   中英

How to verify if my app have ALL_FILES_ACCESS_PERMISSION?

I give all files access by this method :

if (!hasPermissionToManageFiles(myContext)){
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
   try {
        Intent intentFiles = new Intent();
        intentFiles.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
        Uri uriFiles = Uri.fromParts("package", myContext.getPackageName(), null);
        intentFiles.setData(uriFiles);
        myContext.startActivity(intentFiles);
        }
        catch (Exception e){
        Intent intentFiles = new Intent();
        intentFiles.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
        myContext.startActivity(intentFiles);
        }
       }
     }

This line in my manifest :

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

I tried to create method to verify this kind of permission but it's not working. I don't have any error, when my app has the permission to access the intent still appears.

here is my method :

public static boolean hasPermissionToManageFiles(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        return ActivityCompat.checkSelfPermission(context, Manifest.permission.MANAGE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    }
    return true;
}

根据文档,您应该使用Environment.isExternalStorageManager方法而不是checkSelfPermission

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM