Android中同时有多个警报

时间:2016-02-15 05:43:27

标签: android android-alarms

我坚持这个问题。我已经阅读了许多关于堆栈溢出的解决方案,但这些解决方案都没有解决我的问题。

这是我的代码: 在我的主要活动中,我写了这个 -

    this.context = this;
    Intent alarm = new Intent(this.context, AlarmManager.class);
    boolean alarmRun = (PendingIntent.getBroadcast(this.context,0,alarm, PendingIntent.FLAG_NO_CREATE) != null);
    if (alarmRun == false){
          PendingIntent pending = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
          AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 15000, pending);
            }

因此,第一次打开应用程序时,将调用Broadcast Receiver,即AlarmManager.java。 这是我在闹钟管理器中所做的事情: 在我的onReceive -

AlarmDB db = new AlarmDB (context);

List<Alarm> alarms = db.getAlarm();

if(alarms != null)
{
    for (Alarm alarm : alarms)
    {
         Calendar calendar = Calendar.getInstance();
         final int nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
         final int nowMinute = Calendar.getInstance().get(Calendar.MINUTE);
         final int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);

         calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(alarm.getAlarmHour()));
         calendar.set(Calendar.MINUTE, Integer.parseInt(alarm.getStartTimeMinute()));
         calendar.set(Calendar.SECOND, 00);

          PendingIntent pIntent = createPendingIntent(context, alarm);

          if (!(Integer.parseInt(alarm.getAlarmHour()) < nowHour) && !(Integer.parseInt(alarm.getAlarmHour()) == nowHour && Integer.parseInt(alarm.getStartTimeMinute()) <= nowMinute)) 
          {
                 AlarmManager alarmMgr = (AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
                 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) 
                 {
                       alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
                  } else {
                        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
                  }
          }
      }
}

这是我单独制作的createPendingIntent方法:

private static PendingIntent createPendingIntent(Context context, Alarm m) {
        Intent i = new Intent(context, AlarmService.class);
        i.putExtra(ID, m.getID());
        i.putExtra(NAME, m.getMedName());

        return PendingIntent.getService(context, m.getID(), i, PendingIntent.FLAG_UPDATE_CURRENT);
    }

因此,每当当前时间与数据库中的时间匹配时,意图就会通过我的AlarmManager活动中的服务触发。请注意,在调用getService时,请求代码是数据库中的唯一ID。我的问题是尽管我正在使用一个唯一的id,每次有两个或更多的警报被触发,同时只有其中一个被成功解雇。那我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

if (alarmRun == false){
          PendingIntent pending = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
          AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 15000, pending);
            }

调用setRepeating将导致BroadcastReceiver以15000毫秒的间隔多次触发。请改用setExact