省电模式下的网络连接 [英] Network connection in power saving mode

查看:194
本文介绍了省电模式下的网络连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调查我从用户那里获得的一份报告时,我注意到启用省电模式(Nexus 4,Android 5.1.1)后,下载数据的后台服务在一段时间后出现连接异常.当设备屏幕关闭,未在屏幕打开的情况下对其进行测试时,会发生这种情况.

When investigating one of the reports I got from users I noticed that when the power saving mode is enabled (Nexus 4, Android 5.1.1) the background service that downloads data gets connection exception after some time. This happens when the device screen is off, didn't test it with screen on.

在操作过程中同时保存了 PowerManager.PARTIAL_WAKE_LOCK WifiManager.WIFI_MODE_FULL .我正在使用OkHttp进行连接,并且Wifi一直处于打开状态.

There are hold both PowerManager.PARTIAL_WAKE_LOCK and WifiManager.WIFI_MODE_FULL during the operations. I am using OkHttp for connections and the Wifi was turned on all the time.

除了在这种情况下同时按住两个锁(wifi和partial_wake)外,我还能做什么?这对于用户使这些连接正常工作至关重要.

What else can I do except holding both locks (wifi and partial_wake) in such situation? This is critical for user to have those connections working.

Stacktrace-但是我认为在这里并没有真正的意义

Stacktrace - however it is not really relevant here I think:

W/System.err: java.util.concurrent.ExecutionException:   java.net.ConnectException: Failed to connect to *************
W/System.err:     at java.util.concurrent.FutureTask.report(FutureTask.java:93)
W/System.err:     at java.util.concurrent.FutureTask.get(FutureTask.java:177)
....
W/System.err: Caused by: java.net.ConnectException: Failed to connect to **************
W/System.err:     at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:139)
W/System.err:     at okhttp3.internal.io.RealConnection.connect(RealConnection.java:108)
W/System.err:     at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)
W/System.err:     at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
W/System.err:     at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
W/System.err:     at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
W/System.err:     at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
W/System.err:     at okhttp3.RealCall.getResponse(RealCall.java:240)
W/System.err:     at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
W/System.err:     at okhttp3.RealCall.execute(RealCall.java:57)
...

wifi和移动数据连接均会发生这种情况.我还确认保持了唤醒锁,并且一切正常,除了没有连接是可能的以外,其他所有功能均按预期进行.开启萤幕后,一切正常,但保持萤幕开启不是这里可接受的解决方案

this happens with both wifi and mobile-data connections. I also confirmed that wake locks are being held and everything is working as supposed except the fact that no connection is possible. When screen is on, everything works fine but keeping screen on is not an acceptable solution here

推荐答案

您提到过-这很可能是由于省电"模式引起的.在这种模式下,系统会尝试最小化唤醒调用的数量,并停止它认为不必要的所有进程.

As you mentioned - this is likely because of Power Saving mode. In this mode system tries to minimize the number of wake calls and stops all the processes it considers unnecessary.

幸运的是,Android预见了一种解决方法-称为前台服务".如果服务作为前台启动,则将其视为用户活动,并且在资源不足时不会被删除.

Luckily Android foresees a way to deal with this - it's called Foreground Service. If Service is started as Foreground it will be treated as User activity and will not be a subject for removal when low on resources.

您可以考虑将所有关键活动移至前台,并将所有支持模块(例如,分析)留在后台服务中

You may consider moving all critical activities to Foreground and leave all supportive modules (for example, analytics) in Background services

这篇关于省电模式下的网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆