2

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. Here's what I did in my Alarm Manager: In my 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:

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. Notice that in calling the getService, the request code is the unique id from database. 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. So how should I do this?

Piyush
  • 1,744
  • 1
  • 15
  • 28

1 Answers1

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

Calling setRepeating will cause the BroadcastReceiver to be triggered multiple times at an interval of 15000 milliseconds. Use setExact instead.

Piyush
  • 1,744
  • 1
  • 15
  • 28
  • Call requires API level 19 (current min is 15): android.app.AlarmManager#setExact -- the setExaxt gives off this error. @Piyush – Hunter Winchester Feb 21 '16 at 19:23
  • will this be able to let me fire intents on the same time? – Hunter Winchester Feb 21 '16 at 19:25
  • 1
    "let me fire intents on the same time" Where is this mentioned in the question? – Piyush Feb 21 '16 at 19:28
  • I edited my question on the last part, kindly check again. Thanks – Hunter Winchester Feb 21 '16 at 19:33
  • Yes it will be done. Just call use alarmmanager.set() twice and set time accordingly. – Piyush Feb 21 '16 at 19:33
  • Where particularly should I put the alarmmanager.set()? And how should I put two? Won't it give errors? Or confused the program? – Hunter Winchester Feb 21 '16 at 20:01
  • Call it where you're calling setRepeating(). What is there to be confused? Why exactly do you need 2 alarms to be triggered at the same time? – Piyush Feb 21 '16 at 20:04
  • My application is a medication reminder, so if the user needs to take two kinds of medicines at the same time that's where the 2 alarms triggered at the same time takes place. – Hunter Winchester Feb 21 '16 at 20:07
  • So can't the same reminder handle more than one kind of medicine? – Piyush Feb 21 '16 at 20:08
  • No. The one alarm per medicine is part of the applications requirements. – Hunter Winchester Feb 21 '16 at 20:24
  • "if the user needs to take two kinds of medicines at the same time that's where the 2 alarms triggered at the same time takes place" What if there are 3 kinds of medicines? – Piyush Feb 21 '16 at 20:28
  • Then its three alarms that needs to be fired at the same time. Do you think that's possible? – Hunter Winchester Feb 22 '16 at 03:44
  • 1
    It's entirely possible, but it would be a bad user experience. If on firing alarms, 3 different notifications appear, then I guess It's ok for the user to swipe them out after being reminded. But if there 3 activities launched, the user will be frustrated to dismiss one after the other. Plus scheduling 2 or 3 alarms at the same time is not a good practice. – Piyush Feb 22 '16 at 11:13
  • Can you suggests other t]solution except multiple alarms? since it's a bad practice. – Hunter Winchester Feb 24 '16 at 05:31
  • Create a single alarm, check for different kinds of medicines and alert the user accordingly. If you have to use multiple alarms, use notifications instead. – Piyush Mar 02 '16 at 13:04