How can I programmatically open the permission screen for a specific app on Android 6.0 (Marshmallow)?
According to the official Marshmallow permissions video (at the 4m 43s mark), you must open the application Settings page instead (from there it is one click to the Permissions page).
To open the settings page, you would do
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
This is not possible. I tried to do so, too. I could figure out the package name and the activity which will be started. But in the end you will get a security exception because of a missing permission you can't declare.
UPDATE:
Regarding the other answer I also recommend to open the App settings screen. I do this with the following code:
public static void startInstalledAppDetailsActivity(final Activity context) {
if (context == null) {
return;
}
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + context.getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(i);
}
As I don't want to have this in my history stack I remove it using intent flags.
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
Description
Settings.ACTION_APPLICATION_DETAILS_SETTINGS
Opens Details setting page for App. From here user have to manually assign desired permission.
Intent.FLAG_ACTIVITY_NEW_TASK
Optional. If set then opens Settings Screen(Activity) as new activity. Otherwise, it will be opened in currently running activity.
Uri.fromParts("package", getPackageName(), null)
Prepares or creates URI, whereas, getPackageName() - returns name of your application package.
intent.setData(uri)
Don't forget to set this. Otherwise, you will get android.content.ActivityNotFoundException
. Because you have set your intent as Settings.ACTION_APPLICATION_DETAILS_SETTINGS
and android expects some name to search.
Instead, you can open a particular app's general settings with one line:
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID)));