我使用下面的代码创建和取消一个镜头警报。有时会发生警报没有取消,而是在定义的时间段内触发。字符串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);
}