简体   繁体   English

运行前台服务时设备是否进入打ze模式

[英]Does device enter doze mode while foreground service running

My app needs to run in background for long time and periodically send data to server. 我的应用需要长时间在后台运行,并定期将数据发送到服务器。 I tried AlarmManager but it is restricted in doze mode. 我尝试使用AlarmManager,但是它在打ze模式下受到限制。 I next thought is the foreground service. 我接下来想到的是前台服务。

Does the device enter doze mode while a foreground service is running? 运行前台服务时设备是否进入打ze模式?

If the device enters doze mode, is internet connection restricted while foreground service is running? 如果设备进入打ze模式,前台服务运行时是否限制了互联网连接?

That works for me pretty well, tested several hours of continuous operation with some emulators and Android 6.0 and 7.1 real devices, network connection never lost : 这对我来说效果很好,在一些模拟器和Android 6.0和7.1真实设备上测试了几个小时的连续运行,网络连接从未丢失:

In the Main Activity onPause I acquire a Partial WakeLock and I start a Service with STARTFOREGROUND_ACTION intent, the Service calls startForeground and shows a Notification. 在“暂停时的主要活动”中,我获得了部分WakeLock,并以STARTFOREGROUND_ACTION意图启动了服务,该服务调用startForeground并显示通知。

On Activity onResume the WakeLock is released, the Service stops with STOPFOREGROUND_ACTION intent and the Service itself calls stopForeground & stopSelf. 在活动onResume上释放WakeLock时,服务会以STOPFOREGROUND_ACTION意图停止,并且服务本身会调用stopForeground和stopSelf。

And also that avoids Activity to be destroyed on Back Key press : 而且这还避免了活动在按Back键时被破坏:

 @Override
    public void onBackPressed() {
         moveTaskToBack(true);
    }

Of course I provide a menu option to stop the background porcessing and finish the Activity 当然,我提供了一个菜单选项来停止后台处理并完成活动

My code that periodically ask an internet server is placed in the Activity, it uses a timer. 我的代码(定期询问互联网服务器)放置在“活动”中,它使用计时器。

Hope it will help 希望对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM