Android Marshmallow: Changing permissions at run time crashes app
It's because of additional features added from Marshmallow. You need to request from user at runtime. For this use this class which I have made. Then use it whereever required
public class AppPermission {
public static boolean isMarshmallowPlusDevice() {
return Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1;
}
@TargetApi(Build.VERSION_CODES.M)
public static boolean isPermissionRequestRequired(Activity activity, @NonNull String[] permissions, int requestCode) {
if (isMarshmallowPlusDevice() && permissions.length > 0) {
List<String> newPermissionList = new ArrayList<>();
for (String permission : permissions) {
if (PackageManager.PERMISSION_GRANTED != activity.checkSelfPermission(permission)) {
newPermissionList.add(permission);
}
}
if (newPermissionList.size() > 0) {
activity.requestPermissions(newPermissionList.toArray(new String[newPermissionList.size()]), requestCode);
return true;
}
}
return false;
}
}
Then put this code where you require permission from user.
if (!AppPermission.isPermissionRequestRequired(SignInActivity.this, new String[]{"android.permission.GET_ACCOUNTS"},
REQUEST_APP_PERMISSION)) {
// Your code if permission available
}
After this, in your Fragment
or Activity
put this code -
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_APP_PERMISSION:
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
switch (permission) {
case "android.permission.GET_ACCOUNTS":
if (PackageManager.PERMISSION_GRANTED == grantResult) {
// Your code
}
break;
}
}
break;
}
}
The above code is for request permission for GET_ACCOUNTS
you can change it to whatever required.
Android marshmallow dynamic permission change kills all application processes
That is, for application to work correctly when launched after changing permission, it cannot have a dependency on launcher activity to be started.
That has been the case for years. For example, if your process is terminated due to low memory conditions, but the user has been in it recently (say, within the past half-hour), when the user visits the overview screen (what you call the "UI multi-task menu") and goes to return to your app, control will return to a fresh instance of whatever activity the user had been in last (i.e., had been on the top of the BACK stack).
Is this expected behavior with dynamic permissions on all Android 6.0+ devices?
Yes. It is also expected behavior in all previous Android devices, for other cases where your process was terminated but your task is still outstanding and recent.
Why is there a difference in behavior from when the application process is killed by swiping it out from UI multitask menu?
Swiping a task off the overview screen removes that task. Hence, that task cannot be reused when the user tries returning to your app (e.g., via a home screen launcher icon).
Related Topics
How to Update UI from Asynctask
Displaying Contact Number and Contact Name in a Custom List View
Broadcast Receiver Not Working in Ics If the App Is Not Started Atleast Once
Why Isn't My Vector Drawable Scaling as Expected
Android: How Handle Message Error from the Server Using Volley
Don't Collapse Toolbar When Recyclerview Fits the Screen
How to Pass Parameters to Onclicklistener
Android Marshmallow: Changing Permissions at Run Time Crashes App
How to Get Meid and Imei Information Using Adb Commands on Android 5.0
Force a Camera to Always Open in Portrait Mode in Android
Onnewintent() Lifecycle and Registered Listeners
Cordova Platform Add Android Not Working While Listing Android Targets
Want to Compile Native Android Binary I Can Run in Terminal on the Phone