English • 简体中文 (Simplified Chinese)
Library for Android runtime permissions
- XXPermissions-ktx is the Kotlin extension library for XXPermissions, Easily request permissions using Kotlin DSL.
- Provide Java compatible chain calling, which can also be easily used in Java.
Add JitPack package repository
- If your Gradle version is below
7.0
, add JitPack in your rootbuild.gradle.kts
orbuild.gradle
at the end of repositories:
// build.gradle.kts
allprojects {
repositories {
maven("https://jitpack.io")
}
}
// build.gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
- If your Gradle version is
7.0
or higher,add JitPack in your rootsettings.gradle.kts
orsettings.gradle
at the end of repositories:
// build.gradle.kts
dependencyResolutionManagement {
repositories {
maven("https://jitpack.io")
}
}
// build.gradle
dependencyResolutionManagement {
repositories {
maven { url 'https://jitpack.io' }
}
}
- Add the dependency in your module
build.gradle.kts
orbuild.gradle
:
// build.gradle.kts
dependencies {
implementation("com.github.Zhao-YinGang:XXPermissions-ktx:V1.6.0")
}
// build.gradle
dependencies {
implementation 'com.github.Zhao-YinGang:XXPermissions-ktx:V1.6.0'
}
XXPermissions-ktx is for AndroidX. Add the AndroidX migration configuration in your gradle.properties
:
# Migrate third-party libraries to Android X
android.enableJetifier = true
- Kotlin Basic usage
xxPermissions {
// add location permissions
permissions(Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION)
// request result
onResult { allGranted, grantedList, deniedList ->
toast(
"allGranted: " + allGranted +
"\ngrantedList: " + grantedList +
"\ndeniedList: " + deniedList
)
}
}
- Kotlin Complete Usage
xxPermissions {
// add bluetooth connect permission
permissions(Permission.BLUETOOTH_CONNECT)
// add camera permission
permissions(Permission.CAMERA)
// add location permissions
permissions(Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION)
// If you should show rationale before requesting permissions, this callback will be called
onShouldShowRationale { shouldShowRationaleList, onUserResult ->
// The dialog here is just an example and does not use DialogFragment to handle the dialog lifecycle
AlertDialog.Builder(this@requestDemo)
.setTitle("Request Permissions")
// Provide appropriate prompts based on rationale parameter
.setMessage(
"The application requires the following permissions: " +
permissionsNames(shouldShowRationaleList) +
"If these permissions are denied, some functions will be restricted."
)
.setNegativeButton("Cancel") { dialog, _ ->
dialog.cancel()
}
.setPositiveButton("OK") { dialog, _ ->
dialog.dismiss()
// User clicked positive button, call onConsent callback to requesting permissions
onUserResult.onResult(true)
}
.setOnCancelListener {
onUserResult.onResult(false)
}
.show()
}
// permissions denied permanently
onDoNotAskAgain { doNotAskAgainList, onUserResult ->
// The dialog here is just an example and does not use DialogFragment to handle the dialog lifecycle
AlertDialog.Builder(this@requestDemo)
.setTitle("Permissions permanently denied")
// Provide appropriate prompts based on denied parameter
.setMessage(
"The following permissions have been permanently denied: " +
permissionsNames(denied.doNotAskAgainList) +
"Please enter the settings screen to grant permissions."
)
.setNegativeButton("Cancel") { dialog, _ ->
dialog.cancel()
}
.setPositiveButton("Enter Settings") { dialog, _ ->
dialog.dismiss()
// User clicked positive button, call onConsent callback to entering settings screen
onUserResult.onResult(true)
}
.setOnCancelListener {
onUserResult.onResult(false)
}
.show()
}
// request result
onResult { allGranted, grantedList, deniedList ->
toast(
"allGranted: " + allGranted +
"\ngrantedList: " + grantedList +
"\ndeniedList: " + deniedList
)
}
}
- Java Complete Usage
public class RequestDemo4j {
public static void run(Activity activity) {
XXPermissions4j.with(activity)
// add bluetooth connect permission
.permissions(Permission.BLUETOOTH_CONNECT)
// add camera permission
.permissions(Permission.CAMERA)
// add location permissions
.permissions(Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION)
// If you should show rationale before requesting permissions, this callback will be called
.onShouldShowRationale((shouldShowRationaleList, onUserResult) -> {
// The dialog here is just an example and does not use DialogFragment to handle the dialog lifecycle
new AlertDialog.Builder(activity)
.setTitle("Request Permissions")
// Provide appropriate prompts based on rationale parameter
.setMessage(
"The application requires the following permissions: " +
RequestDemoKt.permissionsNames(rationale.getRationaleList()) +
"If these permissions are denied, some functions will be restricted."
)
.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel())
.setPositiveButton("OK", (dialog, which) -> {
dialog.dismiss();
// User clicked positive button, call onConsent callback to requesting permissions
onUserResult.onResult(true);
})
.setOnCancelListener(dialog -> onUserResult.onResult(false))
.show();
})
.onDoNotAskAgain((doNotAskAgainList, onUserResult) -> {
// The dialog here is just an example and does not use DialogFragment to handle the dialog lifecycle
new AlertDialog.Builder(activity)
.setTitle("Permissions permanently denied")
// Provide appropriate prompts based on denied parameter
.setMessage(
"The following permissions have been permanently denied: " +
RequestDemoKt.permissionsNames(denied.getDoNotAskAgainList()) +
"Please enter the settings screen to grant permissions."
)
.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel())
.setPositiveButton("OK", (dialog, which) -> {
dialog.dismiss();
// User clicked positive button, call onConsent callback to entering settings screen
onUserResult.onResult(true);
})
.setOnCancelListener(dialog -> onUserResult.onResult(false))
.show();
})
// request result
.onResult((allGranted, grantedList, deniedList) -> ToastHelper.toast(activity,
"allGranted: " + allGranted +
"\ngrantedList: " + grantedList +
"\ndeniedList: " + deniedList
))
// request permissions
.request();
}
}
To learn how to use XXPermissions-ktx, please refer to demo。
- XXPermissions - Android permission request framework, adapted to Android 13
Feel free to dive in! Open an issue or submit PRs.
XXPermissions-ktx follows the Contributor Covenant Code of Conduct.
Apache-2.0 © Zhao-YinGang