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)
}
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...