简体   繁体   English

下载线程中的PARTIAL_WAKE_LOCK与SCREEN_DIM_WAKE_LOCK

[英]PARTIAL_WAKE_LOCK vs SCREEN_DIM_WAKE_LOCK in download thread

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. 所以我认为可以获得一个partial_wake_lock,只是为了让cpu处理下载。 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. 然后我尝试了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? 我的问题是,为什么我不能保持我的线程运行时,像在screen_dim_wake_lock期间一样持有partial_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? 是因为我不是在UI线程上运行吗?

EDIT: 编辑:

I added a wifilock. 我添加了一个wifilock。 The wifi doesn't go to sleep, but my thread still dies. wifi没有进入睡眠状态,但我的线程仍然死亡。 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. 我想知道我是否可以阻止这一点。

I had the same problem with you with one service that continuously download a large set of URLs. 我有一个同样的问题,一个服务不断下载大量的URL。 The best way was to use a screen_dim_wake_lock as you did and bring your service to the foreground, as described here . 最好的办法是像你一样使用screen_dim_wake_lock,把你的服务前台,描述在这里

In such a way the screen is always on, so the same happens and with your WI-FI interface. 通过这种方式,屏幕始终处于打开状态,因此使用WI-FI界面也是如此。 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! 相反,如果你让手机闲置一段时间然后屏幕关闭,手机将进入睡眠模式,15分钟后WIFI也将关闭!

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..): 所以,另一种方法是使用partial_wake_lock(只有CPU打开)并更改手机的设置,以便WIFI始终开启(可能这看起来像你获得的WIFI_LOCK ..):

  • from home screen go to System or Settings and then 从主屏幕转到系统或设置然后
  • select Wireless & Networks 选择无线和网络
  • select WI-FI settings 选择WI-FI设置
  • press Menu and choose Advanced 按菜单,然后选择高级
  • press Wi-Fi Sleep Policy and change it to Never 按Wi-Fi睡眠政策并将其更改为从不

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连接一段时间,那么Android可能会因为它不活动而终止你的服务..但是如果你的手机总是连接到WIFI网络并且稳定性很好那么一切都会正常工作!

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

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