How to use AccessController in android?

2019-05-31 12:55发布

问题:

In an Android application (API level 10),I do not use below permission in manifest:

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

So application would not write on root of external storage.But when I try to check read/write permission via AccessController by this code:

File f = Environment.getExternalStorageDirectory();
String path = f.getAbsolutePath();
String actions = "read,write";
try {
    AccessController.checkPermission(new FilePermission(path, actions));
    Log.d("Tag", "You have read/write permition to use : " + path);
} catch (AccessControlException e) {
    Log.d("Tag", "You have not read/write permition to use : " + path);
} catch (SecurityException e) {
    Log.d("Tag", "You have not read/write permition to use : " + path);
}

Result will be:

You have read/write permition to use : /storage/sdcard0

So why AccessController.checkPermission does not throw exception on root of External Storage when application has no permission to write on it?