Home Android Requesting for Permissions at Run Time in Android 6.0 (Marshmallow)

Requesting for Permissions at Run Time in Android 6.0 (Marshmallow)

82
2
SHARE

In Android 6.0 (API level 23), users grant permissions to apps while the app is running, and not when they install the app. While requesting permissions at run time, the user does not need to grant permissions when they install or update the app. It additionally provides the user additional management over the app’s functionality; for example, a user might value more highly to a camera app access to the camera however to not the device location. Like this, the user will limit the permission to associate degree app.

Android is a advantage afar operating system, i.e. anniversary appliance in android is afar from addition through a audible id, and anniversary appliance book / abstracts is clandestine to that appliance only. Anniversary Android appliance is started in its own action appropriately are abandoned from all added applications (even from arrangement/ absence applications). As a aftereffect an Android appliance can’t admission any book or abstracts alfresco its ambit until and unless the book or abstracts is aggregate with the application.

1. Check For Permissions

Checking for permissions is necessary because the app will be running if the particular permission is granted to operation which requires that permission.The user is consistently chargeless to abjure the permission, so even if the app acclimated the area yesterday, it can’t accept it still has that permission today. So, checking for it is a good approach.

To check for the app having permission, call the ContextCompat.checkSelfPermission() method. For example, this piece shows a way to check if the activity has permission to access the location:

// Assume yourActivity is the current activity (Your Activity name)
int permissionCheck = ContextCompat.checkSelfPermission(yourActivity,
        android.Manifest.permission.ACCESS_FINE_LOCATION);

If the app has the permission, the adjustment allotment PackageManager.PERMISSION_GRANTED, and the app can advance with the operation. If the app does not accept the permission, the adjustment allotment PERMISSION_DENIED, and the app has to absolutely ask the user for permission.

 

2. Request the permissions you need

After checking for permissions, if your app does not have the permission it needs, the app must call one of the requestPermissions() method to request the appropriate permissions.

The following code checks if the app has permission to access the device location, and requests the permission if necessary:

final private int PERMISSIONS_REQ=123;
// Here, yourActivity is the current activity
if (ContextCompat.checkSelfPermission(yourActivity,
                android.Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {

    if (ActivityCompat.shouldShowRequestPermissionRationale(yourActivity,
            android.Manifest.permission.ACCESS_FINE_LOCATION)) {

    } else {

        ActivityCompat.requestPermissions(yourActivity,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSIONS_REQ);

        // PERMISSIONS_REQ is an
        // app-defined int constant.
    }
}

 

3. Handle the permissions request response

A dialog box appears to the user when your app requests permissions. When the user responds, the system invokes your app’s onRequestPermissionsResult() methodology, passing it the user response. The recall is passed a similar request code you passed to requestPermissions().

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSIONS_REQ: {
            
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {

                // permission refused
            }
            return;
        }
    }
}

permissions

2 COMMENTS

  1. I see you don’t monetize your blog. You can make extra income,
    your niche is good for one method, just
    search in google; reselling online by Riggso

LEAVE A REPLY

Please enter your comment!
Please enter your name here

13 − 8 =