android – AlarmManager不重复
作者:互联网
我正在编写一个“简单”的通知器,其中包括调用网站,检查响应并通知是否有新内容.
我正在使用服务来执行http操作,我希望AlarmManager以给定的频率重复调用服务.我一直在检查像this这样的教程和其他示例,因为我希望每当用户离开设置屏幕(到目前为止唯一的Activity)并且在BOOT完成之后安排服务,所以我创建了一个类到包装调度代码.
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;
}
}
这是从设置Activity调用调度程序
//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接收器工作正常,但它从不重复服务调用.
关于问题在哪里的任何想法?
提前致谢.
解决方法:
我找到了答案here.
显然,当您想要安排SERVICE时,不要使用PendingIntent.getBroadcast而是使用PendingIntent.getService.
只是那个小小的改变,它正在重复它应该.
希望这有助于别人:)
标签:android,service,alarmmanager,android-alarms,repeatingalarm 来源: https://codeday.me/bug/20190529/1176914.html