Request Permissions at Runtime in Android

Request Permissions at Runtime in Android

Android contains a permission system. Permission is the right given to an application to allow access to restricted resources. If an application wants to use restricted resources, the app should request the appropriate permissions.

Permissions can be categorized by protection levels.

Protection levelDescription
NormalPermissions which have a little risk to the user's privacy. These permissions are declared in the app manifest and granted automatically at install time without the user approval.
SignatureThese permissions are declared in the app manifest and granted automatically at install time only if the app that attempts to use a permission is signed with the same certificate as the app which declared the permission.
DangerousPermissions which have a high risk to the user's privacy and can affect the stored information. These permissions are declared in the app manifest and app must prompt the user to grant permissions at runtime.

If an application needs a permission, it should be declared in the app manifest by using a <uses-permission> element, as a child of the top-level <manifest> element.

app/src/main/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application>
        ...
    </application>

</manifest>

Android 6.0 (API level 23) and higher versions of the platform, requires that dangerous permissions should be granted by user at runtime.

The ContextCompat.checkSelfPermission() method allows checking if the user has already granted a particular permission. This method returns PERMISSION_GRANTED or PERMISSION_DENIED.

The ActivityCompat.requestPermissions() method can be used to request permissions.

After the user responds to the system permissions dialog, the system calls the onRequestPermissionsResult() method. It allows checking if permission was granted or denied.

Request code allows identifying permission request.

app/src/main/java/com/example/app/MainActivity.kt

package com.example.app

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity()
{
    private val permission: String = Manifest.permission.ACCESS_FINE_LOCATION
    private val requestCode: Int = 1

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (ContextCompat.checkSelfPermission(this, permission)
            != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this, arrayOf(permission), requestCode)
        } else {
            Log.d("MY_APP", "Permission already granted")
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int,
                                            permissions: Array<out String>,
                                            grantResults: IntArray
    ) {
        when (requestCode) {
            this.requestCode -> {
                if (grantResults.isNotEmpty()
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED
                ) {
                    Log.d("MY_APP", "Permission granted")
                } else {
                    Log.d("MY_APP", "Permission denied")
                }
            }
        }
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.