Question

I really like the section on AlarmManager in the CWAC Advanced Android book. I am now worried about my app's work being killed because it doesn't have a wake lock. Here's what I am worried about. Is my worry rational?

See code below. What if doWakefulWork has to start another service? In this case, the other service needs to acuire a WakeLock to keep running, however the is a gap in time between the service starting and onHandleIntent ending where the wakelock could be released!

@Override
final protected void onHandleIntent(Intent intent) {
 try {
  doWakefulWork(intent)
  }
 finally {
   getLock(this).release();
 }
}

My going answer is: doWakefulWork must execute on the same thread, it can't start other threads or services unless the other services are also WakefulIntentService instances.

Am I right?

Was it helpful?

Solution

If you want your other services to hold a WakeLock, then yes they must either be a WakefulIntentService OR acquire the WakeLock by themselves. If you are worried about the time between when you send the intent and when the other service actually acquires the lock you'll need to implement some type of blocking mechanism (ie block the doWakefulWork method from ending until the other service has started and aquired the lock)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top