my Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
....
<service
android:name=".service.LocationService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="location"
android:stopWithTask="true"/>
</application>
</manifest>
Then my code for requesting permissions in my Main activity is as follows:
ActivityResultLauncher<String[]> permissionRequest =
registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {
boolean hasAllPermissions = true;
for (Boolean value : result.values()) {
hasAllPermissions = hasAllPermissions && value;
}
if (hasAllPermissions){
startLocationService();
}
else {
needsPermissionsDialog.show();
}
});
private void checkPermissions() {
permissionRequest.launch(new String[]{
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.POST_NOTIFICATIONS,
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_BACKGROUND_LOCATION,
Manifest.permission.FOREGROUND_SERVICE,
Manifest.permission.FOREGROUND_SERVICE_LOCATION
});
}
private void startLocationService(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (locationService == null) {
Intent locationServiceIntent = new Intent(MainActivity.this, LocationService.class);
startForegroundService(locationServiceIntent);
bindService(locationServiceIntent, locationServiceConnection, 0);
}
}
else {
Toast.makeText(this, "Missing location permission", Toast.LENGTH_SHORT).show();
}
}
On older android it works 100% with no problems, the service is started, I can bind to it, and get location updates.
On Android 14 however ActivityResultContracts.RequestMultiplePermissions()
result has false
for the ACCESS_FINE_LOCATION
permission.
When I turn off the permission in the (android) settings for the app, it asks for the permission (see image) but it only grants it for the ACCESS_BACKGROUND_LOCATION
, and the ACCESS_FINE_LOCATION
result still remains false.
If I remove the ACCESS_BACKGROUND_LOCATION
from the manifest, the Location permission option in the (android) settings for the app is gone. If remove it from the permissionRequest.launch()
list, I do not get any prompt to allow location access.
It also does not matter If I add/replace FINE with ACCESS_COARSE_LOCATION
anywhere (list or manifest). It also always gets false as a result.
All other permission requesting dialogs work fine.
The problem was replicated on 2 Xiaomi and 1 Pixel device, so it is not even device specific.
P.S. I know this is my 3rd question in this topic, but this is also my 3rd implementation, And still no result.