Many months ago, there was a discussion about how to use the
AlarmManager effectively to trigger a service.  The summary is to use
a BroadcastReceiver to receive the Intent sent by the AlarmManager,
and pass WakeLocks from the BroadcastReceiver to the Service.  I
understand that, and am modifying the app I work on (K-9 Mail) to act
accordingly.  (Although I'm using a somewhat different technique,
described at the end.*)

But what about Intents sent from one Service to another, or from an
Activity to a Service?  What do we need to do about WakeLocks in those
cases?  If the first entity (let's say it is a Service), holds a
WakeLock in onStart, but then calls startService() with an Intent
destined for another Service, it should then release its WakeLock
before the completion of onStart, how do I make sure that the phone
stays awake long enough for the second Service to start?

If the Services are in a single process, I could pass a WakeLock using
a static between the two, but that seems to eliminate the nice loose
coupling that Intents provide, and prevents me from running my
Services in two processes, or even calling a Service in a different
application.  Does Android provide any guarantee that the onStart()
method of a Service will execute when an Intent is sent to it from
another application?  Or that the CPU will remain awake long enough
for the recipient service to at least acquire its own WakeLock?

I did a lot of searching and reading before making this post, and
didn't find any documentation that explicitly discusses this topic.
I'd be happy to be pointed in the right direction.

Thanks,
Dan.
K-9 Dog Walker

* So, my BroadcastReceiver keeps a static Map<Long, WakeLock> of
WakeLocks it creates when onReceive is called.  It creates a new
WakeLock for each call to onReceive, and generates a new unique ID
(hence the Long) for each one.  The ID is sent as an Extra in the
Intent to the Service being fired.  The Service, in onStart, creates
its own WakeLock, then sends a "release wakelock" Intent back to the
BroadcastReceiver with the same ID.  When the BroadcastReceiver gets
that returned Intent, it releases the WakeLock stored in the map.
This method is a bit more complex than the public static, but does
allow the BroadcastReceiver and Service to reside in different
processes.  Please let me know if this technique is harmful or bad in
some way.  It seems to work for me.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to