Android doze mode and foreground service

2020-05-07 00:14发布

问题:

I read many questions here about doze mode like Android doze mode, Android N Doze mode - how to keep background download and processing?, Wakelock and doze mode and many others. But in many replies, opinions is different. I can't test it by myself (emulator does not support my processor, and my only test device is android 5.1). I would like to know the next situation. I have an activity, which controls the media player. On devices with android 6.0 and higher it wouldn't work as expected cause of doze mode. As many solutions says to prevent doze mode it should start a service in separate process in foreground and control media player from there. The question is: if i start foreground service with partial wake lock in separate process, which would do nothing except showing notification, and leave controls to media player in my activity, should it prevent the doze mode? Or maybe there any other ways except keep the screen on from window manager?

回答1:

According to this SO question and answer:

In this post's comments on Sep 17 Dianne Hackborn writes:

Apps that have been running foreground services (with the associated notification) are not restricted by doze.

It looks like using a foreground service prevents the app from being killed by Doze.

Please note though, that some vendors (I know of Samsung, but there are probably others) create their own RAM conservation/battery saving tools. Not even caring about Doze. You are not guaranteed to get the same behaviour on these devices.