Hi There,
In our recent article, we have been looking into the ActivityResult API in android, this API was introduced as an alternative to the deprecated startActivityForResult and onActivityResult callback, along with that we found out that onRequestPermissionsResult is also deprecated, a callback used to capture result back after the user responded to runtime permission prompt. In this article, we will be learning about how to get runtime permission in android with ActivityResult API
You can learn in detail about the Android Activity Result API in this article:
[Tutorial] Get Android Activity Result in Kotlin With ActivityResult API
What is a Runtime Permission In Android:
Runtime permissions, also known as dangerous permissions, give your app additional access to restricted data, and they allow your app to perform restricted actions that more substantially affect the system and other apps. Therefore, you need to request runtime permissions in your app before you can access the restricted data or perform restricted actions. When your app requests runtime permission, the system presents a runtime permission prompt
Source: Permission on Android | Android Developers
As explained in the android developer's docs, runtime permission aka dangerous permission is to ask the user for confirmation in order to use some of the features from his device, for example, access to the files stored in the device or access to the user's current location, back in the days these permissions were informed to the user during install time, to increase the security and privacy of the user these runtime permissions were introduced, this helps protect the user's data and privacy because without the user's knowledge no application can access any of the data which require this permission, few examples are storage, location, camera, and microphone. You can the developer's docs for more information on which permissions are runtime permissions.
How to ask runtime permission in android with kotlin:
Now that we know what are runtime permission and how that's going to impact our application, there are some times where we might need this permission in our application lets to say we want to store some data in the user's devices for him to easily access is next time or when opens the application without internet, we don't want the user to stare at an empty white screen right, so for that purpose, we need to ask the user for permission to access to their storage.
And that's now easy with the ActivityResult API as it helps us achieve with less code and verify type safety, and we can also say that for these purposes the ActivityResult API was introduced. The ActivityResult API provides some pre-defined contracts that help us to request permission which confirms type safety. We share a detailed article on ActivityResult API with Custom Contract Here
How to use ActivityResult API to Get Runtime Permission
private val requestStoragePermission =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions())
{ permissionGranted ->
if (permissionGranted.isNotEmpty()) {
if (permissionGranted[permissionArray[0]]!! &&
permissionGranted[permissionArray[1]]!!) {
permissionStatusTextView.text = "Permission Granted"
} else {
permissionStatusTextView.text = "User denied permission"
}
}
}
private val permissionArray = arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
Start the Runtime Permission Workflow to get Access:
private fun checkStoragePermission() {
if (isAllPermissionGranted()) {
permissionStatusTextView.text = "Permission Already Granted"
} else {
if (requireActivity().shouldShowRequestPermissionRationale(permissionArray[0])
|| requireActivity().shouldShowRequestPermissionRationale(permissionArray[1])
) {
Toast.makeText(
requireContext(),
"In order to demonstrate the success scenario we
need you to allow access to the permission",
Toast.LENGTH_LONG
).show()
requestStoragePermission.launch(permissionArray)
} else {
requestStoragePermission.launch(permissionArray)
}
}
}
0 Comments
Have doubts? Shoot your thoughts....