问题
im so confused reading this and trying to figure out if foreground services will run when the device goes into deep doze mode. Could someone clarify. I want to know on marshmallow and upwards if foreground services can continue to run. I always thought ALL THREADs are suspended even foreground services when device sleeps.
I see the doze mode restrictions but nothing about foreground services. im so confused if my services outlives doze mode security settings.
From what i can tell in the doze mode restrictions just network calls are stopped. But lets say i was doing some long running main thread work, it means it can continue to run right ? even in doze mode ?
回答1:
Foreground services do not kill in doze mode and it's a great workaround to override the doze mode. Killing the foreground service is highly depending on Mobile OS. Like Huawei, it kills the foreground services after a slack of time and you will not be able to determine the period. Some other phones kill the oldest foreground service if it detects unexpected battery consumption. last year, I spent around 6 months observing phones behavior in killing the foreground services when doze mode is activated or not. and I tried more than one solution to override the doze mode in detecting location every 10 seconds and the best one is the foreground service. So you will face unexpected behavior on some phones but it is the best solution for doze and standby mode. You can see this article and you can also look at this tutorial
回答2:
Doze mode is for saving your battery. You should put your app in white list For deactivate doze mode.
Source : https://developer.android.com/training/monitoring-device-state/doze-standby
Support for other use cases Almost all apps should be able to support Doze by managing network connectivity, alarms, jobs, and syncs properly, and by using FCM high-priority messages. For a narrow set of use cases, this might not be sufficient. For such cases, the system provides a configurable whitelist of apps that are partially exempt from Doze and App Standby optimizations.
An app that is whitelisted can use the network and hold partial wake locks during Doze and App Standby. However, other restrictions
still apply to the whitelisted app, just as they do to other apps. For example, the whitelisted app’s jobs and syncs are deferred (on API level 23 and below), and its regular AlarmManager alarms do not fire. An app can check whether it is currently on the exemption whitelist by calling isIgnoringBatteryOptimizations().
And Here how to insert your app to white list : 1. Step --> Add this permission in your xml file.
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
2.Step İgnore battery optimizations
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
来源:https://stackoverflow.com/questions/56866806/doze-mode-do-foreground-services-continue-to-run