0

I have a few questions regarding the WakefulIntentService implementation by CWAC:

1) Is it safe to use multiple WakefulIntentServices at the same time within my application?

2) Is it still ok to use my own code that handles AlarmManager? This would save me re-implementing my alarm handling code. Currently, I have a class with static methods and variables which are used by other classes within the application to set the alarm. My AlarmReceiver then starts the WakefulIntentServices by classing doWakefulWork().

This class is fantastic work!

Thanks

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
jtnire
  • 1,348
  • 5
  • 21
  • 32

1 Answers1

3

Is it safe to use multiple WakefulIntentServices at the same time within my application?

You should only need one. Use different Intent information (e.g., actions, extras) to distinguish the different commands.

I have not tried multiple distinct services -- while it is possible that it will work, I cannot guarantee it.

Is it still ok to use my own code that handles AlarmManager?

Oh, sure. Follow the "Basic Usage" instructions, calling sendWakefulWork() on WakefulIntentService when you want the work to be done. Just bear in mind that you must do that from a BroadcastReceiver's onReceive() if AlarmManager is the trigger -- that's an AlarmManager requirement.

This class is fantastic work!

I am glad that you like it!

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I do indeed do that from a onReceive :). I actually do need multiple WakefukIntentServices, as the services required are distinctly separate from each other that will probably need to run simultaneously. What would be the reasons that it wouldn't work? Are the WakeLocks static and potentially shared between different WakefulIntentService classes? Thanks – jtnire Mar 16 '12 at 23:27
  • @jtnire: "Are the WakeLocks static and potentially shared between different WakefulIntentService classes?" -- the `WakeLock` is static, but that should be OK. "What would be the reasons that it wouldn't work?" -- because it hasn't been tried, and things that haven't been tried sometimes don't work, particularly when written by a Murphy. :-) Eventually, I'll write a `PooledIntentService` and a `WakefulPooledIntentService` that behave like `IntentService` but use a thread pool instead of a single thread, which would allow you to do all of this with one service. – CommonsWare Mar 16 '12 at 23:36
  • On second thought, maybe I don't need to have two WakefulIntentServices, as the order wasn't guaranteed in the first place :) However one question remains, what if another developer uses WakefulIntentService? My use wouldn't interfere with theirs, would it? That's sounds like a really noob question :) – jtnire Mar 16 '12 at 23:51
  • @jtnire: "what if another developer uses WakefulIntentService? My use wouldn't interfere with theirs, would it?" -- no, because you will be in separate processes. Your `WakeLock`, etc. will be completely separate from theirs. – CommonsWare Mar 16 '12 at 23:52
  • Just wanted to say thank you for your help and for your idea to only use one service. I can't believe that I didn't think about this myself! Such a simple concept, yet makes it so much easier to deal with concurrency issues, because now I *know* that *only* one thread will ever be inside the WakefulIntentService (right??). – jtnire Mar 17 '12 at 00:22
  • @jtnire: Yes, `IntentService` only has one background thread, and `WakefulIntentService` does not change that. – CommonsWare Mar 17 '12 at 00:49