if (Build.VERSION.SDK_INT >= 23) {
if (checkPermission()) {
Log.e("permission", "Permission already granted.");
} else {
requestPermission();
}
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(SolutionBrouchereActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
viewOrDownloadPDF();
return true;
} else {
return false;
}
}
private void requestPermission() {
ActivityCompat.requestPermissions(getParent(), new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
}
This functions not worked after denying permission on the splash screen and not able to open permission prompt dialog in another activity.
You can call the
shouldShowRequestPermissionRationale
method in your newActivity
if user did not grant the permission on Splash Screen.Reference: https://developer.android.com/training/permissions/requesting
Put this function in common file and call whenever you use it will again check
If your
target SDK version is >=23
then you need to ask for permissions at run time. Otherwise Android will not ask for permission like old Android does.If this is the case then you should be able to see that no permissions have been granted if you go to
Settings > Apps > "Your app" > Permissions
.If you don't want to ask permissions you can reduce your target sdk version to 22 to get the old permission system. You can still compile with sdk version 23 though.
So for >=23 Use this code:
This code will automatically detect which dialog should be shown, because there is a particular limit in android to show that permission dialog. If particular number of attempts made then it will not show permission dialog, and we will have to move user to settings it self.
This code will help you to achieve both scenarios :
CustomPermissionManager.java :
Call it like this :
You can add other permission too in future like here STORAGE_PERMISSION is added, by adding same type of method.