0

I have two Repeating alarms in my app each with different time and notifications. This alarms are called when the user registered a play card.

When the play card is registered this two alarms must notify the user each day.

1.) 2:30pm - Time to exceed

2.) 3:30pm - Time to comeback

Now the problem is each time I set the two alarms the alarms keeps firing right away but when i just set one alarm it works just fine.

The codes for the alarm

 public void setAlarmRepeating()
{
    player_database = new player_database(MainActivity.this);
    Db_retrieve db = new  Db_retrieve(MainActivity.this);
    db.openDB();
    c = player_database.queryData("select * from player_ID ORDER BY ID DESC");
    try {
        if (c != null && c.moveToNext()) {
            Calendar calendar1=Calendar.getInstance();
            calendar1.set(Calendar.HOUR_OF_DAY,14);
            calendar1.set(Calendar.MINUTE,30);
            calendar1.set(Calendar.MILLISECOND,0);
            Intent intent=new Intent(MainActivity.this,Dog_alarm.class);
            PendingIntent dog1=PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarm1=(AlarmManager)getSystemService(ALARM_SERVICE);
            alarm1.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),calendar1.getTimeInMillis(),dog1);


            Calendar calendar2=Calendar.getInstance();
            calendar2.set(Calendar.HOUR_OF_DAY,15;
            calendar2.set(Calendar.MINUTE,30);
            calendar2.set(Calendar.MILLISECOND,0);
            Intent intent1=new Intent(MainActivity.this,Dog_alarm.class);
            PendingIntent dog2=PendingIntent.getBroadcast(this,2,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarm2=(AlarmManager)getSystemService(ALARM_SERVICE);
            alarm2.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),calendar2.getTimeInMillis(),dog2);
            }
            else
             {
            Toast.makeText(MainActivity.this,"No pets registered",Toast.LENGTH_SHORT).show();
            }


        } catch (SQLException e) {
        e.printStackTrace();
    }
}

Then on my BroadcastReceiver I did this

public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager1=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
    Intent dog_intent1=new Intent(context,HomeActivity.class);
    PendingIntent dog_pending1=PendingIntent.getActivity(context, 1,dog_intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder1=new NotificationCompat.Builder(context)
            .setContentIntent(dog_pending1)
            .setSmallIcon(R.drawable.ic_action_prof_breed_icon)
            .setSound(uri)
            .setContentTitle("Pentra")
            .setContentText("Time to exceed")
            .setAutoCancel(true);
    notificationManager1.notify(1,builder1.build());

    NotificationManager notificationManager2=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
    Intent dog_intent2=new Intent(context,HomeActivity.class);
    PendingIntent dog_pending2=PendingIntent.getActivity(context, 2,dog_intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    Uri uri1= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder2=new NotificationCompat.Builder(context)
            .setContentIntent(dog_pending2)
            .setSmallIcon(R.drawable.ic_action_prof_breed_icon)
            .setSound(uri1)
            .setContentTitle("Pentra")
            .setContentText("Time to go back?")
            .setAutoCancel(true);
    notificationManager2.notify(2,builder2.build());
}

if you can look it up, please tell me what im doing wrong, been pondering about this for 1 day now and still don't know what is wrong.

Edit:

I tried to add this to check if the day sa pass or not

  if(calendar1.before(Calendar.getInstance())) {
                calendar1.add(Calendar.DATE, 1);
            }

but still the alarm keeps firing away

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • If one of your alarms fires immediately, you're setting it for a time that's already passed for the day. We don't know what time it is where you are, so you'll have to check that yourself. – Mike M. Mar 02 '18 at 07:07
  • ah ok so I'll need to first check if the day time has right? – Richard Denser Mar 02 '18 at 07:21
  • Yep. If you're just wanting to set it for the next day then, you can do something like is shown in this answer: https://stackoverflow.com/a/36536228. – Mike M. Mar 02 '18 at 07:23
  • Tried testing it with a different time now with the edit code above but still it keeps firing at the same time, I really don't know why its keeps firing – Richard Denser Mar 02 '18 at 07:36
  • What about `calendar2`? – Mike M. Mar 02 '18 at 07:37
  • Oh, hold on. Your `setInexactRepeating()` calls are wrong, too. The second argument is the time for the first alarm - which you're setting at `System.currentTimeMillis()` - and the third argument is the interval - e.g., `AlarmManager.INTERVAL_DAY` - not your figured times. After the time adjust, they would be something like `alarm1.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, dog1);`. – Mike M. Mar 02 '18 at 07:40
  • I already did the same with calendar2 it was the same. I really don't know why it keeps firing right away but when ive removed alarms2 and just set alarms1 it was working fine so i guess theres something wrong this alarms2? – Richard Denser Mar 02 '18 at 07:42
  • ah ok ill try that now – Richard Denser Mar 02 '18 at 07:43
  • Now the alarm doesn't fire now right away which is what i want but now the problem is that the alarms will start at the same time if the 2nd alarm starts. – Richard Denser Mar 02 '18 at 08:01
  • How are you determining that? Just from the `Notification`s? 'cause no matter which alarm fires, you're going to get both at once. In your `BroadcastReceiver`, you're unconditionally posting both. – Mike M. Mar 02 '18 at 08:06
  • ah so you mean I need another BroadcastReceiver for ther notification is that right? – Richard Denser Mar 02 '18 at 08:10
  • No, you can do it with one, with either different actions, or extras. Might as well use actions, in this case. When you create the `Intent`s for the alarm, call `setAction()` on them with two different `String`s, then check `getAction()` in the Receiver to determine which `Notification` to post. For example, on the alarm: `Intent intent=new Intent(...).setAction("dog1");`. Similarly for `"dog2"`. Then in `onReceive()`, `if (intent.getAction().equals("dog1")) { // Post the first one } else { // Post the second }`. – Mike M. Mar 02 '18 at 08:19
  • Ah thank you very much sir its now working fine ah just a simple question sir if i need to cancel the alarms all I have to do is call this right alarmmanager.cancel(pendingIntent)? – Richard Denser Mar 02 '18 at 08:33
  • Yep. And you don't have to save that particular `PendingIntent` you're creating to set the alarm. As long as you create one that's exactly the same, it'll work. – Mike M. Mar 02 '18 at 08:36
  • 1
    Thank you very much sir. – Richard Denser Mar 02 '18 at 08:38

0 Answers0