Run time permission in Android

Starting from Android 6.0 (API 23), users are not asked for permissions at the time of installation rather developers need to request for the permissions at the run time. Only the permissions that are defined in the manifest file can be requested at run time.


Declare the permission in Android Manifest file: In Android permissions are declared in AndroidManifest.xml file using the uses-permission tag.






Modify activity_main.xml file to Add two buttons to request permission on button click: Permission will be checked and requested on button click. Open activity_main.xml file and add two buttons in it.


activity_main.xml


<!--Button to request storage permission-->
 <Button
     android:id="@+id/storage"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Storage"
     android:layout_marginTop="16dp"
     android:padding="8dp"
     android:layout_below="@id/toolbar"
     android:layout_centerHorizontal="true"/>
 <!--Button to request camera permission-->
 <Button
     android:id="@+id/camera"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Camera"
     android:layout_marginTop="16dp"
     android:padding="8dp"
     android:layout_below="@id/storage"
     android:layout_centerHorizontal="true"/>


Check whether permission is already granted or not. If permission isn’t already granted, request user for the permission

// Function to check and request permission
public void checkPermission(String permission, int requestCode)
{
  
    // Checking if permission is not granted
    if (ContextCompat.checkSelfPermission(
            MainActivity.this,
            permission)
        == PackageManager.PERMISSION_DENIED) {
        ActivityCompat
            .requestPermissions(
                MainActivity.this,
                new String[] { permission },
                requestCode);
    }
    else {
        Toast
            .makeText(MainActivity.this,
                      "Permission already granted",
                      Toast.LENGTH_SHORT)
            .show();
    }
}

Override onRequestPermissionsResult() method: onRequestPermissionsResult() is called when user grant or decline the permission. RequestCode is one of the parameteres of this function which is used to check user action for corresponding request. Here a toast message is shown indicating the permission and user action.


// This function is called when user accept or decline the permission.
// Request Code is used to check which permission called this function.
// This request code is provided when user is prompt for permission.
  
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String[] permissions,
                                       @NonNull int[] grantResults)
{
    super
        .onRequestPermissionsResult(requestCode,
                                    permissions,
                                    grantResults);
  
    if (requestCode == CAMERA_PERMISSION_CODE) {
  
        // Checking whether user granted the permission or not.
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  
            // Showing the toast message
            Toast.makeText(MainActivity.this,
                           "Camera Permission Granted",
                           Toast.LENGTH_SHORT)
                .show();
        }
        else {
            Toast.makeText(MainActivity.this,
                           "Camera Permission Denied",
                           Toast.LENGTH_SHORT)
                .show();
        }
    }
    else if (requestCode == STORAGE_PERMISSION_CODE) {
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(MainActivity.this,
                           "Storage Permission Granted",
                           Toast.LENGTH_SHORT)
                .show();
        }
        else {
            Toast.makeText(MainActivity.this,
                           "Storage Permission Denied",
                           Toast.LENGTH_SHORT)
                .show();
        }
    }
}



Comments