StartaskPermissions
StartaskPermissions is a library that helps to handle runtime permissions on Android, entirely written using Kotlin language.
Using in your projects
Gradle
The library is published to JitPack repository.
- Add the JitPack repository to your root build.gradle at the end of repositories.
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
- Add the dependency
dependencies {
implementation "com.github.illiashenkoo:startask-permissions:${latest.version}"
}
Usage with Kotlin
- Add the following line to AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA" />
- Create a
Permission
object
private val permission: Permission by lazy {
Permission.Builder(Manifest.permission.CAMERA)
.setRequestCode(MY_PERMISSIONS_REQUEST_CODE)
.build()
}
- Check and request permission if needed
permission.check(this)
.onGranted {
// All requested permissions are granted
}.onShowRationale {
// Provide an explanation if the user has already denied that permission request
}
- Delegate the permission handling to library
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
permission.onRequestPermissionsResult(this, requestCode, grantResults)
.onGranted {
// All requested permissions are granted
}.onDenied {
// Oops, some of the permissions are denied
}.onNeverAskAgain {
// Oops, some of the permissions are denied
// User chose "never ask again" about a permission
}
}