Search code examples
androidcommonsware-cwac

How to set two different WakefulIntentService AlarmListeners?


I'm using Commonsware's WakefulIntentService (https://github.com/commonsguy/cwac-wakeful) to trigger a periodic job in my Android application (updating a cache of images). So far so good, but I want to add a different WakefulIntentService, to do a different job, at a different time interval.

I'm not sure if I can do this and how. At the moment, I'm scheduling an alarm like this:

WakefulIntentService.scheduleAlarms(new CacheAlarmListener(), mContext, true);

The implementation of CacheAlarmListener is this:

@Override
public void scheduleAlarms(AlarmManager alarmManager, PendingIntent pendingIntent, Context context) {
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60000, AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
}

@Override
public void sendWakefulWork(Context context) {
    WakefulIntentService.sendWakefulWork(context, CacheRefreshService.class);
}

Looking through the source code, the getListener(Context) method from AlarmReceiver seems to find and trigger a single AlarmListener. Am I wrong, or is there another way to schedule an alarm or setting a different PendingIntent?

Basically, I would like to be able to register a new, separate AlarmListener, or at least be able to figure out which WakefulIntentService to send the work to, in the sendWakefulWork(Context) method.


Solution

  • So far so good, but I want to add a different WakefulIntentService, to do a different job

    You do not need to do that. A single WakefulIntentService can do multiple things, based on different Intent characteristics (e.g., action strings, extras). Having multiple WakefulIntentService implementations in the same project is untested -- and unless somebody can convince me of its necessity, is unsupported.

    the getListener(Context) method from AlarmReceiver seems to find and trigger a single AlarmListener. Am I wrong

    No, you are correct.

    is there another way to schedule an alarm or setting a different PendingIntent?

    Follow the "Basic Usage" instructions on the project's README.

    or at least be able to figure out which WakefulIntentService to send the work to

    Please only have one WakefulIntentService.

    I need to be able to trigger two different services.

    Please only have one WakefulIntentService.