I have just started working with runtime permissions on Android M. I have CAMERA, READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE declared in AndroidManifest.xml
Th
add this in your onCreate
method of activity :-
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
0);
}
}
and override
this method :-
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Again check for permission in onRestart().
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onRestart() {
super.onRestart();
checkForPermission();
}
@TargetApi(Build.VERSION_CODES.M)
private void checkForPermission() {
int permissionCheck = checkSelfPermission(Manifest.permission.READ_CONTACTS);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Granted");
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_CONTACTS)) {
Log.d(TAG, "Contacts Permission Required!!");
createSnackbar("Contacts Permission Required!!", "Try Again");
}
ActivityCompat.
requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CONTACT);
}
}
Check out this link for further information: https://github.com/sagarjogadia28/PermissionSample/
You need to do different things.
First, ask for permission what you are trying to perform. Example camera:
//This is a class variable
private final static int MY_PERMISSIONS_REQUEST_CAMERA = 1;
...
// before use camera:
// check permissions for CAMERA
if (ContextCompat.checkSelfPermission(ActivityName.this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
//request permission
ActivityCompat.requestPermissions(ActivityName.this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
} else {
//permissions granted, do wahtever with camera
functionToHandleCamera();
}
Then you need to handle the user response: allow or deny. So Override onRequestPermissionsResult method
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permissions granted, do wahtever with camera
functionToHandleCamera();
} else {
// permission denied
Toast msg = Toast.makeText(this, "PERMISSIONS NEEDED", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, 0, 0);
msg.show();
}
return;
}
}
}
More info: https://developer.android.com/training/permissions/requesting.html
EDIT: if you only want to check if the permission is granted or not, you onlu need the first part of the code I've wrote.
You have to change the targetSDKVersion because
You're supposed to get PERMISSION_GRANTED for any targetSdkVersion below 23, even if the user toggled off that permission group in Settings. I suggest that you uninstall the app completely, set your targetSdkVersion to 23, and try it again.
you can check this link