2

I'm trying to test the Runtime Permissions specifically for Android sdk > 23. But my app is being granted permissions automatically without asking.

Note : I'm using sdk version 24. Here's a snippet of code I'm using:

public void onCalendarClick(View view) {
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager
            .PERMISSION_DENIED) {
       if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_CALENDAR)) {
            //Display Explanation to the user
            //For granting permissions to the app.
        }
        else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, CALLBACK_CALENDAR);
        }
    }
}

@Override
public void onRequestPermissionsResult(int resultCode, String permission[], int grantResults[]) {
    if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Toast toast;
        switch (resultCode) {
            case CALLBACK_CALENDAR : toast = Toast.makeText(this,"Calendar Permission Granted!!",Toast.LENGTH_LONG);
                toast.show(); break;
            //Other Cases
        }
    }
}

When I click on Calendar Button, the onCalendarClick() method run, but without asking for any permission, the App directly displays Calendar Permission Granted!! toast. In the App's settings, there are though No Permissions Granted/Requested being displayed.

Am I missing something or doing it the wrong way? Thanks for any help.

Robin
  • 305
  • 2
  • 13
  • You are asking if the requestCode is CALENDAR, but not if it is really granted, you should check in the permission[] and grantResults[] values to know if its granted or not. – Jonathan Aste Jun 05 '17 at 17:44
  • If I'm not wrong then, I think in the method `onRequestPermissionsResult(...)` there's an `if condition` checking if the permissions are granted or not. @JonathanAste – Robin Jun 05 '17 at 17:47
  • What is your `targetSdkVersion`? – CommonsWare Jun 05 '17 at 17:50
  • It's `sdk version 24` @CommonsWare – Robin Jun 05 '17 at 17:52
  • Try doing a full uninstall and reinstall of the app. Perhaps in earlier testing you granted the permission. – CommonsWare Jun 05 '17 at 17:53
  • If you have already granted the permission, you have to go to App Info -> permissions and unselect the permission you granted before – Jonathan Aste Jun 05 '17 at 18:08
  • It didn't ask for granting permissions, neither on emulator nor on physical device both running on Android 7.0. As far as the App's setting's are concerned, I double checked, there are No permissions granted, but My App is still acting like it is having permissions @JonathanAste – Robin Jun 05 '17 at 18:15
  • Check my answer, what could be happening is that you are getting the permissions for another request, because you are asking first for the grantResults array while you are not sure wihch permission is it. – Jonathan Aste Jun 05 '17 at 18:24

2 Answers2

2

So here it is. I found out that for android sdk > 22, though Runtime permissions are added programatically for your application but you still need to declare your app's permission in the AndroidManifest.xml file. So, after adding the code:

<uses-permission android:name="android.permission.WRITE_CALENDAR"/>

in the AndroidManifest.xml, the app asks for the permission and it's finally working. For more information : Android M permission dialog not showing .Thanks to all for helping me out )

Robin
  • 305
  • 2
  • 13
0

You are missing the order of the code. Check this:

@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
    switch (requestCode) {
        case CALLBACK_CALENDAR: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // calendar-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
    }
}

There is a little difference, you are asking if the permission is granted even before you know you are talking about CALENDAR permissions. So, you should first check if the current permission response is the one you want, and then check if the permission is granted.

Source: https://developer.android.com/training/permissions/requesting.html

Jonathan Aste
  • 1,764
  • 1
  • 13
  • 20