[英]How to check if app has ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION and other storage permissions on lower Androids
Is there a way to check if my device already has the following permissions with one line or how should I check this?有没有办法通过一行来检查我的设备是否已经具有以下权限,或者我应该如何检查?
I am supporting SDK versions from 21 to 30 so I cannot use :我支持从 21 到 30 的 SDK 版本,所以我不能使用:
if (!Environment.isExternalStorageManager()) {
Because it requires API level 30, but my min is 21.因为它需要 API 级别 30,但我的最小值是 21。
This is how I am asking for permissions:这就是我请求权限的方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try {
Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
startActivityForResult(intent, 2296);
} catch (Exception ex){
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivity(intent);
}
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
}
So is there a permission check that would work for all of these?那么是否有适用于所有这些的权限检查?
The answer for me here was to change Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
to Build.VERSION.SDK_INT > Build.VERSION_CODES.Q
and then check permission with !Environment.isExternalStorageManager()
.我的答案是将
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
更改为Build.VERSION.SDK_INT > Build.VERSION_CODES.Q
,然后使用!Environment.isExternalStorageManager()
检查权限。
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
if(!Environment.isExternalStorageManager()){
try {
Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
startActivity(intent);
} catch (Exception ex){
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivity(intent);
}
}
} else {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.