我收到此错误
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
据我通过参考文档了解的内容https://developer.android.com/develop/background-work/services/foreground-services#start我们必须检查权限是否被授予。
我在启动前台服务之前添加了权限检查。
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() 有以下代码。
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);
}
怎么了?
我已经阅读了所有可能的文档,但我唯一得到的是我正在使用 ActivityCompat.checkSelfPermission() ,并且在他们建议使用 PermissionChecker.checkSelfPermission() 的文档中。
如果您有任何可能的解决方案,请告诉我。
好的大家,使用这个方法 PermissionChecker.checkSelfPermission() 解决了我的问题。