Reputation: 71
I'm getting this error
Starting FGS with type location callerApp=ProcessRecord{22f416f 27792:smartsense.co.in.sensephone:remote/u0a255} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION] and the app must be in the eligible state/exemptions to access the foreground only permission
As much as I've understood by referring to the documentation https://developer.android.com/develop/background-work/services/foreground-services#start we have to check for the permissions are granted or not.
I've added a permission check before starting a foreground service.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (Utils.checkLocationPermission(this)) {
ServiceCompat.startForeground(this, AUTO_ATTENDANCE_ID, getNotification(getString(R.string.please_enable_gps), activityPendingIntent), FOREGROUND_SERVICE_TYPE_LOCATION);
} else {
Utils.appendLog(TAG, "Location permissions denied for ASIOFS to start", this);
}
} else {
startForeground(AUTO_ATTENDANCE_ID, getNotification(getString(R.string.please_enable_gps), activityPendingIntent));
}
Utils.checkLocationPermission() has the following code.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
&& PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION);
} else {
return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
}
What's wrong?
I've read every possible document but only thing which I've got is that I'm using ActivityCompat.checkSelfPermission() and in the documentation they've suggested to use PermissionChecker.checkSelfPermission().
If you've any possible solution please let me know.
Upvotes: 0
Views: 51
Reputation: 130
Your app is trying to start a Foreground Service (FGS) with type "location", but it does not have the required permissions.
This means:
1 ->You must declare FOREGROUND_SERVICE_LOCATION in your AndroidManifest.xml.
2 -> You must request ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION at runtime.
1 : Add Required Permissions to AndroidManifest.xml
Make sure your AndroidManifest.xml includes:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<usespermissionandroid:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
Note:
FOREGROUND_SERVICE_LOCATION is new in API 34 and must be added.
FOREGROUND_SERVICE is required for all foreground services.
2 : Declare Foreground Service in AndroidManifest.xml
Inside your tag, add this service declaration:
<service
android:name=".YourForegroundService"
android:foregroundServiceType="location"
android:exported="false"/>
Upvotes: 1
Reputation: 71
Okay everyone, using this method PermissionChecker.checkSelfPermission() fixed my problem.
Upvotes: 0