【发布时间】:2016-07-04 19:15:08
【问题描述】:
我正在尝试使用 AlarmManager 为通知设置警报。当用户选择一个列表项时会设置警报,因此我尝试在每次选择列表项时设置一个单独的警报(将唯一 ID 传递到 pendingIntent)。
用于设置闹钟的代码:
public static void setAlarm(Context context, Movie movie, Schedule schedule){
Intent arg = new Intent(context, NotifyService.class);
arg.putExtra(NotificationHelper.fetch_schedule_id, schedule.getId());
arg.putExtra(NotificationHelper.fetch_movie_id, movie.getId());
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), (int) schedule.getId(), arg, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
am.setExact(AlarmManager.RTC_WAKEUP, schedule.getStart().getTime(), pendingIntent);
else
am.set(AlarmManager.RTC_WAKEUP, schedule.getStart().getTime(), pendingIntent);
}
我正在处理的问题是有时不执行对 set(或 setExact)方法的调用。它确实工作了一半,但在特定情况下,例如,当用户在不到一秒的时间内一个接一个地点击两个或多个列表项时,则只执行第一个调用,其余调用将被忽略。
此外,由于 set/setexact 方法返回 void,因此如果调用了该方法并设置了警报,我将无法调试。现在我必须每次都检查 adb shell dumpsys 警报。
因此,如果有人可以告诉我如何安排警报以便每次都触发它,无论该方法被调用的频率如何,甚至可以引导我朝着正确的方向前进,这都会有很大的帮助。 :)
【问题讨论】:
标签: android notifications alarmmanager