Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check Grants Permissions at Run-Time?

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?

like image 465
Cool Avatar asked May 30 '15 18:05

Cool


People also ask

How do I check permissions granted?

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.

Which run time permissions were introduced?

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.

How do you check all permission is granted in Android?

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.


2 Answers

Nice !!

I just found my need we can check if the permission is granted by :

checkSelfPermission(Manifest.permission.READ_CONTACTS) 

Request permissions if necessary

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;     } 

Handle the permissions request response

@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     } } 
like image 62
Cool Avatar answered Oct 03 '22 11:10

Cool


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; } 
like image 20
Venkatesh Avatar answered Oct 03 '22 11:10

Venkatesh