How Android Runtime Permissions work

What are Android Runtime Permissions?

Android Runtime permissions introduced in Android 6.0 Marshmallow Google changed the way permissions are handled by the application. 

They give users more control over what information apps can access and what actions they can perform.


Here's the gist:

More Privacy for Users: 

Imagine an app that wants to access your location data. With runtime permissions, the app won't get that data until you explicitly permit it. 

Transparency: 

The app has to explain why it needs the permission so you can understand what you're allowing.

Two Types of Permissions: 

There are two main categories: regular permissions (like accessing the internet) that are granted during installation and dangerous permissions (like camera or location) that require your approval at runtime.

In this blog, we will look at new Android run time permissions.


Step 1: In the Android app development, permissions are declared in the AndroidManifest.xml file using the users-permission tag.

<users-permission android:name=”android.permission.PERMISSION_NAME”/>

 

Here we are declaring which permission in the app is needed.

Example below:

<!-- Declaring the required permission-->

<uses-permission android:name=”andorid.permission.READ_EXTERNAL_STORAGE”/>

<uses-permission android:name=”andorid.permission.WRITE_EXTERNAL_STORAGE”/>

<uses-permission android:name=”andorid.permission.CAMERA”/>

Step 2: Check permission first in java or kt file
2.1 check signal permission in your app follow the below syntax.

if(ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) 

// Permission is not granted

 }

2.2  Check multiple permissions in your app follow the below syntax.

ActivityCompat.requestPermissions(MainActivity.this, permissionArray, requestCode)

{

// permissionArray is an array of type String.

}

Step 3: Request permission to open the permission dialog of the user on the application.

private fun checkPermission(permission: String, requestCode: Int) {

if (ContextCompat.checkSelfPermission(this@MainActivity, permission) == PackageManager.PERMISSION_DENIED) { // Requesting the permission ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode) 

} else { 

Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT).show() } 

}

 

Step 4: Override onRequestPermissionsResult() method: onRequestPermissionsResult() this method is get code of permission decline 

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { 

super.onRequestPermissionsResult(requestCode, permissions, grantResults)

 if (requestCode == CAMERA_PERMISSION_CODE) { 

if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)

 { Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT).show() } 

else 

Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT).show() 

}

 } 

else 

if (requestCode == STORAGE_PERMISSION_CODE) { 

if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT).show() 

}

 else

{ Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT).show()

 } 

}

In conclusion, Android runtime permissions give you more control over your data and help you make informed decisions about what apps can do on your device. 

Hence, follow the best practices when requesting permissions: request permission as per your mobile application requirements, and also check if there are any alternatives available before requesting a particular permission. 



365Bloggy April 25, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive