As we all know from API level 23 (Android 6.0) and above, we need to ask Runtime Permissions in Android App. The following article contains utility methods to ask runtime permission in Kotlin Android. This code also shows an Alert Dialog to the user to allow permissions from App Settings, if the user denies permission in App. The screenshots are the following.
Ask Runtime Permission in Kotlin Denial of Runtime Permission
Android categories permissions into 2 major types, Normal and Dangerous permissions. For normal permissions, we don’t need to ask them at runtime, only declaration in the AndroidManifest.xml
file is enough. But for dangerous permissions, only declaration in the AndroidManifest file is not enough, we also need to ask them at runtime. At this Link, you can check the list of normal & dangerous permissions.
Utility Methods to Ask Runtime Permissions in Kotlin
Add the following methods in your Activity or Fragment, where you want to ask runtime permissions. In this code, READ_EXTERNAL_STORAGE
permission is considered. You can change it with your required permission.
fun isPermissionsAllowed(): Boolean {
return if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
false
} else true
}
fun askForPermissions(): Boolean {
if (!isPermissionsAllowed()) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this as Activity,Manifest.permission.READ_EXTERNAL_STORAGE)) {
showPermissionDeniedDialog()
} else {
ActivityCompat.requestPermissions(this as Activity,arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),REQUEST_CODE)
}
return false
}
return true
}
override fun onRequestPermissionsResult(requestCode: Int,permissions: Array<String>,grantResults: IntArray) {
when (requestCode) {
REQUEST_CODE -> {
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission is granted, you can perform your operation here
} else {
// permission is denied, you can ask for permission again, if you want
// askForPermissions()
}
return
}
}
}
private fun showPermissionDeniedDialog() {
AlertDialog.Builder(this)
.setTitle("Permission Denied")
.setMessage("Permission is denied, Please allow permissions from App Settings.")
.setPositiveButton("App Settings",
DialogInterface.OnClickListener { dialogInterface, i ->
// send to app settings if permission is denied permanently
val intent = Intent()
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
val uri = Uri.fromParts("package", getPackageName(), null)
intent.data = uri
startActivity(intent)
})
.setNegativeButton("Cancel",null)
.show()
}
This is how to call the above method
if (askForPermissions()) {
// Permissions are already granted, do your stuff
}
That’s it. Enjoy š
You can also find other useful helping articles, code snippets and tutorials in our Coding Articles & Tutorials Knowledge Base.
Iād must examine with you here. Which is not one thing I usually do! I take pleasure in reading a publish that can make individuals think. Also, thanks for permitting me to comment!
When someone writes an article he/she maintains the thought of a user in his/her mind
that how a user can know it. Therefore that’s why this article is great.
Thanks!
Bookmarked!, I like your web site!