Use Accompanist - Permissions.
Dependecies
dependencies {
implementation "com.google.accompanist:accompanist-permissions:0.20.2"
}
Edit AndroidManifest.xml
<manifest ...>
<!-- Include ACCESS_COARSE_LOCATION as well if you need ACCESS_FINE_LOCATION -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application ...>
</application>
</manifest>
RequireLocationPermission
Composable
@ExperimentalPermissionsApi@Composablefun RequireLocationPermission( navigateToSettingsScreen: () -> Unit, content: @Composable() () -> Unit) { // Track if the user doesn't want to see the rationale any more. var doNotShowRationale by rememberSaveable { mutableStateOf(false) } // Permission state val permissionState = rememberMultiplePermissionsState( listOf( android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION ) ) when { permissionState.allPermissionsGranted -> { content() } // If the user denied the permission but a rationale should be shown, or the user sees // the permission for the first time, explain why the feature is needed by the app and allow // the user to be presented with the permission again or to not see the rationale any more. permissionState.shouldShowRationale || !permissionState.permissionRequested -> { if (doNotShowRationale) { Text("Feature not available") } else { Column { Text("Need to detect current location. Please grant the permission.") Spacer(modifier = Modifier.height(8.dp)) Row { Button(onClick = { permissionState.launchMultiplePermissionRequest() }) { Text("Request permission") } Spacer(Modifier.width(8.dp)) Button(onClick = { doNotShowRationale = true }) { Text("Don't show rationale again") } } } } } // If the criteria above hasn't been met, the user denied the permission. Let's present // the user with a FAQ in case they want to know more and send them to the Settings screen // to enable it the future there if they want to. else -> { Column { Text( "Request location permission denied. " + "Need current location to show nearby places. " + "Please grant access on the Settings screen." ) Spacer(modifier = Modifier.height(8.dp)) Button(onClick = navigateToSettingsScreen) { Text("Open Settings") } } } }}
Usage
@Composablefun test() { val context = LocalContext.current RequireLocationPermission(navigateToSettingsScreen = { context.startActivity( Intent( Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", context.packageName, null) ) ) }) { Text("Location Permission Accessible") }}