How to set two different WakefulIntentService AlarmListeners?

混江龙づ霸主 提交于 2019-12-08 13:41:51

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/14956626/how-to-set-two-different-wakefulintentservice-alarmlisteners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!