2

I am setting alarm usng this

    Calendar now = Calendar.getInstance();
    Calendar alarm = Calendar.getInstance();
    alarm.set(Calendar.HOUR_OF_DAY,21);
    alarm.set(Calendar.MINUTE,30);
    if (alarm.before(now)) {
        alarm.add(Calendar.DAY_OF_MONTH, 1);  //Add 1 day if time selected before now
    }
         AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context,Receiver.class);
   PendingIntent pi = PendingIntent.getBroadcast(context,(int)alarm.getTimeInMillis(),i,0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (int)alarm.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi);

But even if I schedule it for next day,it triggers immediately after saving alarm. Dont know what the issue is have searched a lot but everyone else gets it working

Akki
  • 123
  • 2
  • 12

1 Answers1

1

You are casting a long timestamp to int thus losing bits and changing the actual timestamp value. You end up with a time that already has passed so it executes the intent immediately.

PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

Notice that I removed the (int) cast in the last line.

whitebrow
  • 2,015
  • 21
  • 24
  • I changed it and it didn't triggered immediately ,but it's not even getting triggered at the scheduled time – Akki Sep 25 '16 at 10:46