1
1

fun checkPermission(context: Context):Boolean {
return (ContextCompat.checkSelfPermission(context,android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED )&& (ContextCompat.checkSelfPermission(context,android.Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED)
}

==============================
var hasLocationPermission by remember {
mutableStateOf(checkPermission(this))
}
if (hasLocationPermission.not()){
LaunchLocationPermission(){
hasLocationPermission=true
}
}

==================================
@Composable
fun LaunchLocationPermission(onPermissionGranted: () -> Unit) {
val locationPermissionLauncher = rememberLauncherForActivityResult(contract =ActivityResultContracts.RequestMultiplePermissions(), onResult = {permissions:Map<String,Boolean> ->
var isGranted=true
permissions.entries.forEach {
if (it.value.not()) {
isGranted=false
return@forEach
}
}
if (isGranted) {
onPermissionGranted()
}
})
SideEffect {
locationPermissionLauncher.launch(arrayOf(android.Manifest.permission.ACCESS_COARSE_LOCATION,android.Manifest.permission.ACCESS_FINE_LOCATION))
}

}

  • You must to post comments
Showing 0 results
Your Answer

Please first to submit.