Jimmy
Jimmy

Reputation: 10361

Does a Thread running from a service with wakelock require wakelock?

I want to run a thread to do some work in the background (from a service that acquired a wakelock). after my thread finish work then it will stop the service.

My concerns are :

1- Do i need to require a wakelock in the thread that was started in the service?

2- Can the system kill my thread while leaving my service running?

3- if 2 is the case how could i stop my service (can i give the service a timeout time that it will die after it)

Upvotes: 1

Views: 1507

Answers (1)

mborsuk
mborsuk

Reputation: 1316

  1. WakeLocks are, as I understand applied to the Android component (e.g. service, activity, etc.) and all its associated threads. So you would not have to aquire a new wakelock in your thread. You could prove this by passing the aquired lock and calling isHeld from the started thread.

  2. You should consider a partial wakelock, this will concern only keeping the CPU running, regardless of the screen or keyboard activity. Since this is a service I assume you don't care whether the screen stays on.

Note that it states here: If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers and even after the user presses the power button. In all other wakelocks, the CPU will run, but the user can still put the device to sleep using the power button.

Edit: Also, even though you can do it the way you suggested, it might be better to only aquire and release the wakelock in the child thread for battery consumption purposes. And if you are needing to use WiFi for this work a WiFiLock may also be required.

Upvotes: 2

Related Questions