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. 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?
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.
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..):
- from home screen go to System or Settings and then
- select Wireless & Networks
- select WI-FI settings
- press Menu and choose Advanced
- press Wi-Fi Sleep Policy and change it to Never
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!
Take a look at the Wifi lock, along with a partial lock :