Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 6.0 needs restart after granting user permission at runtime

I follow this guide to ask for permissions for Android 6.0, https://developer.android.com/preview/features/runtime-permissions.html#support-lib

However, I have to destroy my application and re-launch to actually have the WRITE_EXTERNAL_STORAGE permission, otherwise it keeps failing for file creation.

Am I missing something?

if(ActivityCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED){
    ActivityCompat.requestPermissions((MainActivity)mContext,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else {
    Intent i = new Intent(v.getContext(), ServiceDownload.class);
    v.getContext().startService(i);
}

File is created in the service and this is emulator not a real device.

like image 553
SmT Avatar asked Sep 21 '15 15:09

SmT


People also ask

How can I tell if an Android user is denied permission?

Android provides a utility method, shouldShowRequestPermissionRationale() , that returns true if the user has previously denied the request, and returns false if a user has denied a permission and selected the Don't ask again option in the permission request dialog, or if a device policy prohibits the permission.

How do I set permissions in Android programmatically?

You need to declare the permissions in the manifest file first before you request for them programmatically. refer this for more information. declaring the permission in the manifest file is static type declaration by which your os know what kind of permission your app might require.

How do I check if permission is granted Android?

checkSelfPermission(activity, Manifest. permission. X) checks if any permission is already granted, if you check out other answers they do the same, and rest of the code asks for the permissions not granted.


1 Answers

It is a bug as someone already mentioned, but I found in this question that you can add this line:

android.os.Process.killProcess(android.os.Process.myPid());

after your permission has been granted. This is not a real fix, just helps avoid crashing.

like image 187
Drag0 Avatar answered Sep 24 '22 04:09

Drag0