Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner questions: Wakelocks

I am new to the notion of WakeLock and need your help.

Questions:

  1. I assume WakeLock to be some type of lock which when held by the executing code prevents the device from sleeping. What if the device is already in sleep/standby mode, will the code execute then? Assuming that it would never acquire a WakeLock?

  2. When a long running task(abt 7-8 sec) is done in a background thread(AsyncTask) should I be bothered about holding a WakeLock? Does AsyncTask already acquire it for me?

  3. links to official documentations and writeup on wakelocks are appreciated.

Thanks.

like image 874
Manish Khot Avatar asked Nov 23 '10 03:11

Manish Khot


2 Answers

1.If the phone is in full sleep mode, aside from an incoming phone call, you could use an AlarmManager intent to wake the phone up.

From the AlarmManager class documentation:

The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes. If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.

2.If you're working with an AsyncTask, you will want to publish results on to the UI thread on onPostExecute()

From the AsyncTask documentation:

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

3.I suggest you have a read at the official documentation of Power Manager which gives a good introduction to the WakeLock concept.

like image 83
SteD Avatar answered Oct 01 '22 21:10

SteD


  1. Typically the only code that would run while the phone is sleeping is a BroadcastReceiver. Actually, the phone wakes up for a second, runs the BroadcastReceiver's code and sleeps again. Since you should never run long code in a BroadcastReceiver (use it to launch a Service instead), you can basically assume that your code is never run while the phone is sleeping. Of course, if you are using a BroadcastReceiver to start a Service, you should usually obtain a WakeLock.

  2. With an AsyncTask initiated by the user, you don't really need to worry about WakeLocks. It is unlikely the phone will sleep while it is running. I'm not sure if they get a WakeLock, but putting my phone to sleep while running a standard AsyncTask doesn't seem to interrupt the it.

  3. As SteD said, check this out: http://developer.android.com/reference/android/os/PowerManager.html

Basically the only time you need to worry about WakeLocks is when you either expect your task to be interrupted by sleeping (as is the case if you set an alarm that wakes the phone up briefly) or if you absolutley cannot have the task interrupted. Otherwise, just make sure that you gracefully handle any interruptions.

like image 24
Computerish Avatar answered Oct 01 '22 22:10

Computerish