Multiple Runtime Permissions in Android with Kotlin using Dexter
Last Updated :
18 Feb, 2022
From MarshMallow android introduced runtime permissions, we can grant the permissions while using the app, instead of while installing the app. In general, If you want to use a camera or make a call from your app you will ask for the user's permission. There are some predefined functions provided to ask and know that the user has given the permissions or not.
Suppose, we need the user permissions we will ask the permission by using the below function
Kotlin
requestPermissions(arrayOf(Manifest.permission.READ_PHONE_STATE),
Manifest.permission.WRITE_EXTERNAL_STORAGE
ABConstants.REQUEST_CODE_FOR_PERMISSIONS)
Here, we are asking arrayOf permissions to user and once the user is allowed/deny then we will get the result as a callback like below.
Kotlin
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
In this call back we can check, whether the user has given the permissions or not. If not given, again we have to ask him by taking the user to the settings page. The user may grant only one permission and remain not, then it is very difficult to handle and we need to write so much logic code for that. To simplify these kinds of problems, we use the Dexter library. Just add this library in Gradle like below.
implementation 'com.karumi:dexter:6.2.2'
How Does it Work?
1. In the activity add one global variable and checkThePermissions function.
Kotlin
class MainActivity : AppCompatActivity() {
lateinit var dexter : DexterBuilder
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkThePermissions()
}
}
2. Let's implement the function here like below
Kotlin
private fun getPermission() {
dexter = Dexter.withContext(this)
.withPermissions(
android.Manifest.permission.CAMERA,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_PHONE_STATE
).withListener(object : MultiplePermissionsListener {
override fun onPermissionsChecked(report: MultiplePermissionsReport) {
report.let {
if (report.areAllPermissionsGranted()) {
Toast.makeText(this@MainActivity, "Permissions Granted", Toast.LENGTH_SHORT).show()
} else {
AlertDialog.Builder(this@MainActivity, R.style.Theme_AppCompat_Dialog).apply {
setMessage("please allow the required permissions")
.setCancelable(false)
.setPositiveButton("Settings") { _, _ ->
val reqIntent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.apply {
val uri = Uri.fromParts("package", packageName, null)
data = uri
}
resultLauncher.launch(reqIntent)
}
// setNegativeButton(R.string.cancel) { dialog, _ -> dialog.cancel() }
val alert = this.create()
alert.show()
}
}
}
}
override fun onPermissionRationaleShouldBeShown(permissions: List<PermissionRequest?>?, token: PermissionToken?) {
token?.continuePermissionRequest()
}
}).withErrorListener{
Toast.makeText(this, it.name, Toast.LENGTH_SHORT).show()
}
dexter.check()
}
3. Let's Understand the Code
In the first parameter, we are passing activity context and in the second parameter, we are passing a list of permissions just like arrayOf. In the third parameter, we have to implement the MultiplePermissionsListener which is having an onPermissionsChecked function, in this function we get the report about the permissions that are granted or not. If all the permissions are granted then we will show the toast message is granted else we need to show the dialog which is having a settings button. When user clicking on settings button we will take time to the app setting page, where user can manually allow/deny the permissions. When the user comes back from the settings page, we should know whether he granted it or not. For that, we have to use startActivityForResult and here we are adding code like below
Kotlin
resultLauncher.launch(reqIntent)
We get the OnActivityResult callback like below
Kotlin
private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result -> dexter.check()
}
dexter.check() function check the permissions again, if all permissions are granted then we will show the toast message is granted else will do the same process again.
Similar Reads
Android Runtime Permissions with Dexter using Android Jetpack Compose
Android applications require the usage of hardware devices within the android applications such as microphones or cameras. For using these devices within any android application. Permissions for using this hardware have to be provided to the application. For giving these permissions to the applicati
3 min read
Android Run Time Permissions using Jetpack Compose
There are many features within android applications that require permissions to be granted by the user. For granting these permissions during runtime, runtime permissions are used within android applications. These permissions are called when the user wants to use any specific feature. Before using
6 min read
Material Design Date Range Picker in Android using Kotlin
Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Andro
3 min read
How to Enable Notification Runtime Permission in Android 13?
Android 13 starts a new realm of new app permissions which are focussed on users' privacy and peace of mind, one of which is the permission to send notifications to the user. Notifications are an integral part of the Android Operating System, but when you have tons of apps installed, then receiving
5 min read
How to Create Option Menu in Android using Kotlin?
In this article, we will learn how to create an options menu in the Android app using Kotlin. To have an options menu in an Activity, we need to create a new menu XML file and inflate it using menuInflator.inflate( ) method. In menu.xml we will design the options menu as the requirement of the app.
2 min read
Material Design Date Picker in Android using Kotlin
Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Andro
3 min read
Android - Update Data in API using Volley with Kotlin
Android applications use APIs to get the data from servers in android applications. With the help of APIs, we can add, read, update and delete the data from our database using APIs. We can use Volley and Retrofit for consuming data from APIs within the android application. In this article, we will t
5 min read
How to Create Options Menu for RecyclerView in Android using Kotlin?
RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item that can be custom
6 min read
Send Multiple Data From One Activity to Another in Android using Kotlin
There are multiple ways for sending multiple data from one Activity to Another in Android, but in this article, we will do this using Bundle. Bundle in android is used to pass data from one activity to another, it takes data in key and value pairs. To understand this concept we will create a simple
3 min read
Android - Implement Preference Setting Screen with Kotlin
In many apps, we have seen the Settings screen which is most common in most of the apps. This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at im
5 min read