In Android M (Preview) the user can choose a specific app and retreive specific permission.
So I am asking How to check Grants Permissions at Run-Time?
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.
From beginning with Android 6.0 Marshmallow (API LEVEL 23)( Marshmallow was released on October 5, 2015), Google has introduced a new runtime permission model. Users can then allow or deny the permission, users can also grant or revoke permission anytime from settings even if the app is already installed.
On your phone, open the Settings app. Permission manager. Tap a permission type. If you allowed or denied permission to any apps, you'll find them here.
Nice !!
I just found my need we can check if the permission is granted by :
checkSelfPermission(Manifest.permission.READ_CONTACTS)
if (checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant return; }
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! do the // calendar task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } // other 'switch' lines to check for other // permissions this app might request } }
Try this instead simple request code
https://www.learn2crack.com/2015/10/android-marshmallow-permissions.html
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1; private boolean checkAndRequestPermissions() { int camera = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA); int storage = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE); int loc = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION); int loc2 = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION); List<String> listPermissionsNeeded = new ArrayList<>(); if (camera != PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(android.Manifest.permission.CAMERA); } if (storage != PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE); } if (loc2 != PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION); } if (loc != PackageManager.PERMISSION_GRANTED) { listPermissionsNeeded.add(android.Manifest.permission.ACCESS_COARSE_LOCATION); } if (!listPermissionsNeeded.isEmpty()) { ActivityCompat.requestPermissions(this,listPermissionsNeeded.toArray (new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS); return false; } return true; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With