Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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.

like image 333
Jonathan S. Avatar asked Mar 03 '11 16:03

Jonathan S.


People also ask

What is PARTIAL_ wake_ lock?

PARTIAL_WAKE_LOCK. Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.

What are the wake locks available in android?

What are the wake locks available in android Options 1 FULL WAKE LOCK 2 SCREEN BRIGHT WAKE LOCK 3 SCREEN.

What is partial wake lock in Android?

Partial wake locks are a mechanism in the PowerManager API that lets developers keep the CPU running after a device's display turns off (whether due to system timeout or the user pressing the power button). Your app acquires a partial wake lock by calling acquire() with the PARTIAL_WAKE_LOCK flag.


1 Answers

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!

like image 90
Thanasis Petsas Avatar answered Oct 04 '22 08:10

Thanasis Petsas