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.
Okay everyone, using this method PermissionChecker.checkSelfPermission() fixed my problem.