Permission

In Android development, requesting permissions is a way for an app to ask the user for permission to access certain features or data on their device that are considered sensitive or require explicit user consent. Permissions protect the user's privacy and system resources by ensuring that apps cannot access certain data or perform certain actions without the user's consent.

Types of Permissions

  1. Normal Permissions: These permissions do not directly affect the user's privacy and are automatically granted when the app is installed. Examples include access to the internet and setting the alarm.

  2. Dangerous Permissions: These permissions can give the app access to the user's private data or control over the device that can affect the user. Examples include accessing the camera, contacts, location, and storage. Dangerous permissions require explicit user consent.

Requesting Dangerous Permissions

Since Android 6.0 (API level 23), apps must request dangerous permissions at runtime, rather than at install time. Here's a step-by-step process for requesting permissions:

Declare Permissions in Manifest: Add the required permissions in the AndroidManifest.xml file. For example:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_MEDIA_*" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Make request to oneRequestPermissions, this function can help you to handle request permission and open setting detail app if needed. For example:

val permissions: Array<String>
    get() {
        val basePermissions = arrayOf(
            android.Manifest.permission.CAMERA,
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.ACCESS_FINE_LOCATION,
        )
    
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            basePermissions + arrayOf(
                android.Manifest.permission.POST_NOTIFICATIONS,
            )
        } else {
            basePermissions
    
        }
    }
oneRequestPermissions(
    permissions = permissions,
    requestCode = 1120,
    doIfRationale = {
        // this lambda function is optional
        materialAlertDialog {
            setTitle("Permission Required")
            setMessage("Please allow all permission to continue")
            setPositiveButton("Allow") { _, _ ->
                openPermissionAppSettings(this@PermissionActivity)
            }
            setNegativeButton("Cancel") { dialog, _ ->
                dialog.dismiss()
            }
        }.show()
    },
    doIfGranted = {
        // Do something if all permission granted 
    }
)

Detailed Breakdown

  1. Function Call:

    • oneRequestPermissions is called to handle permission requests. It takes several parameters including an array of permissions, a request code, and optional lambda functions for handling rationales and granted permissions.

  2. Parameters:

    • permissions = permissions: This parameter passes an array of permissions that the app needs to request. The variable permissions should be defined elsewhere in the code.

    • requestCode = 1120: This is an integer request code that will be used in the permission request callback to identify this specific request.

  3. doIfRationale:

    • This is an optional lambda function that is executed if the system decides that it should show a rationale for requesting the permissions. This can happen if the user has previously denied the permissions, but the app still needs them to function correctly.

  4. doIfGranted:

    • This is another optional lambda function that is executed if all requested permissions are already granted. This is where you can place the code that should run when permissions are successfully granted.

Example Use Case

Imagine you have an activity called PermissionActivity that requires multiple permissions, such as access to the camera, contacts, and location. You want to ensure that these permissions are granted to provide a seamless user experience. If the permissions are not granted, you show a dialog explaining why they are needed and direct the user to the app settings if they agree.

Here’s how this setup helps:

  • The oneRequestPermissions function handles checking which permissions are granted and which are not.

  • If permissions are denied and the system suggests showing a rationale, the doIfRationale lambda displays a dialog explaining why the permissions are necessary and offers to open the app's settings.

  • If all permissions are granted, the doIfGranted lambda is executed, allowing the app to continue its functionality seamlessly.

This structured approach ensures that permission requests are handled gracefully and that users are informed about why permissions are needed, improving the overall user experience.

Last updated