简体   繁体   English

Android中同时显示多个警报

[英]Multiple alarms at the same time in Android

I'm stuck on this problem. 我被困在这个问题上。 I've read many solutions on stack overflow but none of these have solved my problem. 我已经阅读了许多关于堆栈溢出的解决方案,但是没有一个解决了我的问题。

Here's my code: In my Main Activity, I wrote this-- 这是我的代码:在我的主要活动中,我写了这个-

    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);
            }

So the first time the app is opened a Broadcast Receiver is called which is the AlarmManager.java. 因此,第一次打开该应用程序时,将调用Broadcast Receiver,即AlarmManager.java。 Here's what I did in my Alarm Manager: In my onReceive-- 这是我在警报管理器中所做的事情:在我的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);
                  }
          }
      }
}

And here's the createPendingIntent method I separately made: 这是我单独制作的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);
    }

So everytime the current time matches the time from the database an intent is fired through a service which is in my AlarmManager activity. 因此,每当当前时间与数据库中的时间匹配时,就会通过AlarmManager活动中的服务触发意图。 Notice that in calling the getService, the request code is the unique id from database. 请注意,在调用getService时,请求代码是数据库中的唯一ID。 My problem is eventhough I am using a unique id, everytime there is 2 or more alarm to be fired at the same time only one of those is sucessfully fired. 我的问题是,尽管我使用的是唯一的ID,但每次同时触发2个或更多警报时,只有其中一个成功触发。 So how should I do this? 那我该怎么做呢?

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);
            }

Calling setRepeating will cause the BroadcastReceiver to be triggered multiple times at an interval of 15000 milliseconds. 调用setRepeating将导致以15000毫秒的间隔多次触发BroadcastReceiver Use setExact instead. 请改用setExact

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM