How to Implement the new Permission model in Android Marshmallow

7:46 AM Nilesh Deokar 0 Comments

Android has revised the permission model in the upcoming release i.e. Android M.
If we target the app for the Android M then we will have to follow the new Permission Model.

Enabling the new permissions model

To enable the new M Developer Preview permissions model, set the app's targetSdkVersion attribute to"MNC", and compileSdkVersion to "android-MNC". Doing so enables all the new permissions features.

For the preview release, you must set minSdkVersion to "MNC" to compile with the preview SDK.


Request permissions if necessary

If the app doesn't already have the permission it needs, the app calls the requestPermissions() method to request the appropriate permission or permissions. The app passes the permission or permissions it wants, and also an integer "request code". This method functions asynchronously: it returns right away, and after the user responds to the dialog box, the system calls the app's callback method with the results, passing the same "request code" that the app passed to requestPermissions().

The following code checks if the app has permission to read the user's contacts, and requests the permission if necessary:

if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (shouldShowRequestPermissionRationale(
            Manifest.permission.READ_CONTACTS)) {
        // Explain to the user why we need to read the contacts
    }

    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


When an app requests permissions, the system presents a dialog box to the user. When the user responds, the system invokes your app's onRequestPermissionsResult(int, String[], int[]) passing it the user response. Your app needs to override that method. The callback is passed the same request code you passed to requestPermissions(). For example, if an app requests READ_CONTACTS access it might have the following callback method:

@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
    }
}
If the user denies a permission request, your app should take appropriate action. For example, your app might show a dialog explaining why it could not perform the user's original request.

When the system asks the user to grant a permission, the user has the option of telling the system not to ask for that permission again. In that case, when an app uses requestPermissions() to ask for that permission, the system immediately denies the request. In this case, the system calls your onRequestPermissionsResult() the same way it would if the user had explicitly rejected your request again. For this reason, your app cannot assume that any direct interaction with the user has taken place.



0 comments: