我创建并取消一次性警报,代码如下。 有时会发生警报未取消但在定义的时间内触发。字符串 timerType 始终相同。
有人可以解释为什么警报并不总是被取消?
谢谢。 弗朗索瓦
public static final void startOneShotAlarm(Context context, int minutes, String timerType) {
long period = 1000*60*minutes;
long firstTime = SystemClock.elapsedRealtime() + period;
Intent intent = new Intent(context, RepeaterDone.class);
intent.setData(Uri.parse(timerType));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Log.d("BAN", "RepeaterStart, >" + Uri.parse(timerType) + "< ctx="+context.toString() + "i=" + intent.toString());
// Schedule the alarm!
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, pendingIntent);
}
private static final void stopOneShotAlarm(Context context, String timerType) {
// Create the same intent, and thus a matching IntentSender, for the one that was scheduled.
Intent intent = new Intent(context, RepeaterDone.class);
intent.setData(Uri.parse(timerType));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Log.d("BAN", "stop, >" + Uri.parse(timerType) + "< ctx="+context.toString() + "i=" + intent.toString());
// And cancel the alarm.
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}