Issue
I need to get the MANAGE_ALL_FILES_ACCESS_PERMISSION
to be able to download and install APK's from my application.
When I was targeting SDK 27 and lower, everything worked fine, when I declared
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in the manifest and asked for permission with ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
But now I need to target SDK 30, meaning that WRITE_EXTERNAL_STORAGE
is deprecated and not working for API versions 29 and 30.
I have been trying to find a workaround and one that I could think of was starting the Intent
to allow users to switch the permission on by themselves but I cannot get it to work.
The code I am using:
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);
startActivity(intent);
} 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);
}
And the error that I get when trying to open intent:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.MANAGE_ALL_FILES_ACCESS_PERMISSION }
The error comes from the catch block but the Exception ex
gives the same error.
What am I doing wrong or is this just not allowed on the current phone I am using (XCover 4s Android 10).
Solution
The answer to getting the storage permission that worked for me.
I had to change the Manifest.permission. strings
so instead of what was written in the question I used.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ActivityCompat.requestPermissions(
activity,
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.MANAGE_EXTERNAL_STORAGE
},
1
);
} else {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
100);
}
Or similarly this could be used :
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
Then to get the prompt to allow user to install Apps from unkown sources
:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!activity.getPackageManager().canRequestPackageInstalls()) {
activity.startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", activity.getPackageName()))), 1234);
} else {
}
}
Answered By - Richard
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.