我正在编写一个“简单”的通知程序,其中包括调用网站、检查响应并通知是否有新内容。
我正在使用服务来执行 http 操作,我希望 AlarmManager 以给定的频率重复对服务的调用。我一直在检查这样的教程和其他示例,并且因为我希望在用户离开设置屏幕(迄今为止唯一的活动)和引导完成后安排服务,所以我创建了一个类包装调度代码。
public class Scheduler {
public static boolean cancelScheduledService(Context ctx, Intent serviceIntent) {
boolean success = true;
Log.v("novUmbria", "Scheduler.cancelScheduledService");
try {
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
am.cancel(
PendingIntent.getBroadcast(
ctx, 0,
new Intent(ctx, NotificadorService.class),
// si ya existe, no genera un 2º
PendingIntent.FLAG_CANCEL_CURRENT
)
);
Log.v("novUmbria", "Scheduler.cancelScheduledService Servicio cancelado");
} catch (Exception e) {
Log.e("novUmbria", "Scheduler.cancelScheduledService Excepción: " + e.getMessage());
success = false;
}
return success;
}
public static boolean scheduleService(Context ctx, Intent serviceIntent, long interval) {
boolean success = true;
Log.v("novUmbria", "Scheduler.scheduleService Servicio ");
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm");
// timeformat.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(
// am.setInexactRepeating(
// AlarmManager.ELAPSED_REALTIME_WAKEUP,
AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
interval,
PendingIntent.getBroadcast(
ctx, 0,
new Intent(ctx, NotificadorService.class),
// si ya existe, no genera un 2º
PendingIntent.FLAG_CANCEL_CURRENT
)
);
Log.v("novUmbria", "Scheduler.scheduleService Servicio programado a las "
+ timeformat.format(cal.getTime())
+ " cada " + (interval / 60000) + " minutos"
);
startService(ctx, serviceIntent);
Log.v("novUmbria", "Scheduler.scheduleService Servicio iniciado"
);
} catch (Exception e) {
Log.e("novUmbria", "Scheduler.scheduleService Excepción: " + e.getMessage());
success = false;
}
return success;
}
public static boolean startService(Context ctx, Intent serviceIntent) {
boolean success = true;
Log.v("novUmbria", "Scheduler.startService");
try {
ctx.startService(serviceIntent);
Log.v("novUmbria", "Scheduler.startService Servicio iniciado");
} catch (Exception e) {
Log.e("novUmbria", "Scheduler.startService Excepción: " + e.getMessage());
success = false;
}
return success;
}
}
这是从设置活动中对调度程序的调用
//Settings Activity
@Override
protected void onStop() {
Log.v("novUmbria", "SettingsActivity.onStop");
Intent serviceIntent = new Intent(getApplicationContext(), NotificadorService.class);
Scheduler.cancelScheduledService(getApplicationContext(), serviceIntent);
long frequency = 1000 * 60 / 2;
Scheduler.scheduleService(getApplicationContext(),
serviceIntent,
frequency
);
Log.v("novUmbria", "SettingsActivity.onStop scheduleService");
super.onStop();
}
事情是:logcat 告诉我该服务已被安排(或者,更好地说,它不会引发异常)并且它是第一次执行。但是,在那之后,无论间隔多长或短,它都不会重复。我尝试了几个标志 RTC、RTC_WAKEUP、ELAPSED_REALTIME 等,但我一无所获。
我的测试设备是完全更新的 Nexus 4。我什至重新启动了它,所以我检查了 BOOT_COMPLETE 接收器工作正常,但它从不重复服务调用。
关于问题出在哪里的任何想法?
提前致谢。