Android M
Permissions
shouldShowRequestPermissionRationale
Android Development
Mobile App Security

Android M Permissions Confused on the usage of shouldShowRequestPermissionRationale function

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Android M, also known as Android 6.0 Marshmallow, introduced significant changes in the app permission model, shifting from installation-time permissions to runtime permissions. One of the key features of this change is the ability for users to review and revoke app permissions at runtime. This article will explore the concept of runtime permissions, focusing on the `shouldShowRequestPermissionRationale()` function, a commonly misunderstood component.

Understanding Android M Permissions

Runtime Permissions Overview

In Android Marshmallow and onwards, permissions are requested at runtime rather than when the app is installed. This means:

  • Apps need to request permission to access certain features at the time they are needed.
  • Users can grant or deny access, providing more control over app permissions.
  • Permissions can be changed in the app's settings.

Role of `shouldShowRequestPermissionRationale()`

The method `shouldShowRequestPermissionRationale()` is a crucial part of the runtime permission model. It provides a way to enhance user experience by explaining the need for certain permissions. Here's what you need to know:

  • Purpose: This method returns `true` if the app should show a rationale for requesting a permission. It returns `false` if the user has denied the permission in the past and selected "Don't ask again" or if the permission request is yet to be made.
  • Usage: It's meant to be used before requesting a permission to provide context. For example, if a user denied a permission previously, your app could choose to display an explanation dialog before asking for the permission again.

Here’s an example to illustrate its practical application:

  • First Permission Request: Initially, this method returns `false` because the user hasn't denied the permission yet.
  • Subsequent Requests: If the user denies the permission once, this method will return `true` on subsequent requests, giving you a chance to explain before re-requesting.
  • Check for "Don't ask again": The method returns `false` if the user selects "Don't ask again" after denying the permission. In this scenario, the rationale workflow should be skipped.
  • Internet Access
  • Vibration
  • Wake Lock
  • `requestPermissions()`: Request app permissions from the user.
  • `onRequestPermissionsResult()`: Handle the result of the permission request.
  • `checkSelfPermission()`: Check the status of a given permission.

Course illustration
Course illustration

All Rights Reserved.