问题
The last couple of days I've been trying to figure out which functionality in a service (background and foreground) stops working when the device falls into sleep without a wake lock. All the documentation only says that a wake lock "keeps the CPU from shutting down", but every single functionality I've tested kept working without a wake lock if I turned the screen off and unplugged the device. I've also checked via adb if another application is holding a wake lock, but the wake lock count is 0.
I've tested CPU heavy math calculations, network requests and playing music over MediaPlayer class. Everything kept working in a background service (below Oreo) without a wake lock (The network request worked on some emulators and on others not, but this was the same with and without a wake lock).
So my question is, what are examples for features that stop working on Android if the device falls into sleep?
Edit: I want to make it clear that I am not talking about DOZE. I am talking about the device going into sleep and powering down the CPU, which is supposed to happen when screen is turned off and now wake lock is acquired.
回答1:
Doze mode does not mean the device is going permanently into the out of maintenance mode. It's more like quick naps between the window of actions. This is how it looks like
And these are the restrictions:
Doze restrictions The following restrictions apply to your apps while
- Network access is suspended.
- The system ignores wake locks.
- Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.
- If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
- Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.
- The system does not perform Wi-Fi scans.
- The system does not allow sync adapters to run.
- The system does not allow JobScheduler to run.
Source: https://developer.android.com/training/monitoring-device-state/doze-standby
To see if your app still works during the Doze mode you can force it. Don't use the emulator for that instead try it on a real device.
Use this to force doze mode
adb shell dumpsys deviceidle force-idle
Unforce it
adb shell dumpsys deviceidle unforce
Reset it
adb shell dumpsys battery reset
More to find here: https://developer.android.com/training/monitoring-device-state/doze-standby#testing_doze
Also keep in mind that the handling of doze and standby mode could be handled differently by the device manufacturer.
As far as I know is that "Sleep mode" is the "Standby Mode". You could also force that by using:
adb shell dumpsys battery unplug
adb shell am set-inactive <packageName> true
and wake it up
adb shell am set-inactive <packageName> false
adb shell am get-inactive <packageName>
Also to be noted when the device is being charged the system goes out of the idle mode.
来源:https://stackoverflow.com/questions/53317861/what-exact-functionality-stops-working-if-a-wake-lock-is-not-acquired-and-the-de