In my Jetpack Compose App, I have a Composable helper to ask for Location Permissions:
@Composable
fun GpsLocationHelper(hasLocationAccess: (Boolean)->Unit){
val context = LocalContext.current
val permissions = arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
var hasLocationPermission by remember {
mutableStateOf(
permissions.all{
ContextCompat.checkSelfPermission(
context,
it
) == PackageManager.PERMISSION_GRANTED}
)
}
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestMultiplePermissions(),
onResult = { permissionsMap ->
hasLocationPermission = permissionsMap.values.reduce { acc, next ->
acc && next
}
val locationEnabled = isLocationEnabled(context) //
hasLocationAccess(hasLocationPermission && locationEnabled)
}
)
LaunchedEffect(key1 = true) {
launcher.launch(
permissions
)
}
}
fun isLocationEnabled(context: Context): Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return LocationManagerCompat.isLocationEnabled(locationManager)
}
Further I have methods to get current location, or to start getting location updates.
On the screens where I need GPS coordinates I call it like this:
GpsLocationHelper(hasLocationAccess = {
if (it) {
getCurrentLocation(context){lat, lng ->
....
}
}
})
It works just fine for older android versions I have tested (9,11,12), but on Android 14
I do not get any popups asking for location permission, nor does the Location option appear in the App settings for permissions (see image below.), thus the
permissionsMap
always contains values of false.
In my Manifest I have the following set up:
<uses-feature android:name="android.hardware.location" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
I have read that there are changes in Android 14 Regarding location permissions, but I have found no working solution so far.
Thanks for your help in advance!