下载线程中的 PARTIAL_WAKE_LOCK 与 SCREEN_DIM_WAKE_LOCK
我有一项服务会生成一个下载线程,有时会下载大文件。我意识到,一旦手机进入睡眠状态,下载线程就会显着减慢,然后停止。
显而易见的补救措施是唤醒锁。所以我认为获取partial_wake_lock就可以了,只是为了让CPU处理下载。但这不起作用,同样的行为,下载速度减慢,然后在屏幕关闭时停止。
然后我尝试了 screen_dim_wake_lock。这一次,屏幕保持打开状态(变暗),下载继续全速进行,直到完成,唤醒锁释放,然后手机进入睡眠状态。
我的问题是,为什么我在持有partial_wake_lock时不能像在screen_dim_wake_lock期间一样保持线程运行?关于这两者之间的差异是否存在一些未记录的行为?可能是因为我没有在 UI 线程上运行?
编辑:
我添加了一个 wifilock。 wifi 没有进入睡眠状态,但我的线程仍然死掉。如果其他进程唤醒系统,下载将以正常速度继续,然后再次减慢。如果我唤醒手机,它会继续以正常速度运行,而当屏幕熄灭时,它会再次减慢速度。似乎该线程被系统推入后台。我想知道我是否可以阻止这种情况。
I have a service that spawns a download thread that downloads sometimes large files. I realized that as soon as the phone went to sleep, the download thread would slow down significantly, then stop.
The obvious remedy, a wakelock. So I would think that it would be ok to acquire a partial_wake_lock, just to keep the cpu processing the download. That doesn't work though, same behavior, the download slows and then stops when the screen turns off.
Then I tried a screen_dim_wake_lock. This time, screen stayed on (dimmed), and the download kept going at fullspeed until it was done, wakelock released, and then phone slept.
My question is, why can't I keep my thread running when holding a partial_wake_lock the same as it does during a screen_dim_wake_lock? Is there some undocumented behavior about the difference between these 2? Is it because I am not running on the UI thread perhaps?
EDIT:
I added a wifilock. The wifi doesn't go to sleep, but my thread still dies. If some other process wakes up the system, the download will continue at normal speed, then slow down again. If I wake up the phone, it will continue at normal speed than will slow down once again when the screen goes out. It seems as though the thread is getting pushed into the background by the system. I wonder if I can prevent that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在一项连续下载大量数据的服务中遇到了与您同样的问题
网址。最好的方法是像您一样使用 screen_dim_wake_lock 并将您的服务提供给
前台,如此处所述< /a>.
通过这种方式,屏幕始终处于打开状态,因此您的 WI-FI 接口也会发生同样的情况。相反,如果您让手机闲置一段时间,15 分钟后屏幕将关闭,手机将进入睡眠模式,WIFI 也会关闭!
因此,另一种方法是使用partial_wake_lock(仅CPU开启)并更改手机的设置,以便WIFI始终开启(也许这看起来像您获得的WIFI_LOCK..):
然后,即使在屏幕关闭的睡眠模式下,您的服务也将运行。这里唯一的问题是:
如果你失去WIFI连接一段时间,那么Android可能会终止你的服务,因为它将处于非活动状态。但是如果你的手机始终连接到稳定性良好的WIFI网络,那么一切都会正常工作!
I had the same problem with you with one service that continuously download a large set
of URLs. The best way was to use a screen_dim_wake_lock as you did and bring your service to
the foreground, as described here.
In such a way the screen is always on, so the same happens and with your WI-FI interface. In contrary, if you leave the phone inactive for a while then the screen will turn off, the phone will get in sleep mode and the WIFI will turn off too, after 15 minutes!
So, another way is to use a partial_wake_lock (only CPU is on) and change the settings of your phone so as the WIFI to be always on (maybe this seems like the WIFI_LOCK you acquires..):
Then your service will work even in sleep mode with the screen turned off. The only problem here is:
if you loose WIFI connectivity for a while then the Android may kill your service because it will be inactive.. But if your phone is always connected to the WIFI network with good stability then all will work fine!
看一下 Wifi 锁以及部分锁:
http ://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html
Take a look at the Wifi lock, along with a partial lock :
http://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html