Search code examples
androidwakelock

Implement Wakelock


I have a Service which has two TimerTask which run on frequency of 2 minutes (set by user preference from 2 to 30 minutes.) .

Now this works fine, but when the phone goes to sleep (lights off and phone under lock) this does not work as per expectations. So I think I need to implement wakelock for this..

If so, what is the best way to use wakelock.

I found that we can use Alarmmanager also ..

Please help and also where in the cose should i implement this ..(inside the oncreate methos or the class extending timetask).

Andoid 2.2 taget version


Solution

  • Generally using Wakelocks is a very, very rare need. It can have a significant impact on battery life and it's easy to make error, in that case wakelock will not be released at all.

    For your purpose the AlarmManager ( http://developer.android.com/reference/android/app/AlarmManager.html ) is the ideal solution. It guarantees that no sleep will be performed during your execution, but between this executions(2 or 30 minutes) device can go to sleep and save a good amount of battery. System will wake-up when it's time to execute your next execution.

    Good luck