private void scheduleAlarms(Context context) {
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intentOnAlaramReceiver = new Intent(context, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, 0);
String listOfIntervalConnection = Utils.getStringFromProperties(context, Properties.SP_LIST_OF_ENABLE_INTERVAL_CONNECTIONS, Properties.ENABLE_AFTER);
long enableAfter = DateUtils.MINUTE_IN_MILLIS * Long.parseLong(listOfIntervalConnection);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + enableAfter, enableAfter, pendingIntent);
}
我试图取消像
这样的闹钟 AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intentOnAlaramReceiver = new Intent(context, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, 0);
alarmManager.cancel(pendingIntent);
但是sometimne有时会被取消。为什么以及如何?我该怎么办?
答案 0 :(得分:1)
在启动服务和取消服务时,您正在PendingIntent中使用不同的Intent实例。
将服务意图设置为实例变量,并在服务的启动和取消中使用它。
答案 1 :(得分:0)
尝试使用PendingIntent.FLAG_UPDATE_CURRENT
而不是0;像这样:
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, PendingIntent.FLAG_UPDATE_CURRENT);