Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android prevent immediate trigger of alarm service if alarm time has passed for the day

The reference for Alarm Manager says that

If the stated trigger time is in the past, the alarm will be triggered immediately.

I am facing this problem in my application. Here is my alarm manager code :

Intent myIntent = new Intent(getActivity(), DinnerAlarmReceiver.class);
                pendingDinnerIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);

                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);

                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(), pendingDinnerIntent);

Is there any workaround to this problem?

-----EDIT------

I have written some code to estimate if the set time for the alarm is before the current time . Here is the above portion with corresponding changes :

Calendar calendar = Calendar.getInstance();
                long currentTime = calendar.getTimeInMillis();
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);
                long setTime = calendar.getTimeInMillis();
                Timestamp setTimestamp = new Timestamp(setTime);
                Timestamp currentTimestamp = new Timestamp(currentTime);
                if (setTimestamp.after(currentTimestamp))
                {
                    alarmManager.set(AlarmManager.RTC_WAKEUP,
                            calendar.getTimeInMillis(), pendingDinnerIntent);
                }
                else
                {
                }

What should I the alarmManager to in case setTimestamp is before currentTimestamp ?

like image 811
Flame of udun Avatar asked Apr 10 '16 21:04

Flame of udun


People also ask

How do I stop my Android phone from repeating an alarm?

Open your phone's Clock app . At the bottom, tap Alarm. On the alarm you want, tap the Down arrow . Cancel: To cancel an alarm scheduled to go off in the next 2 hours, tap Dismiss.

Does alarm Manager work when app is closed?

android - AlarmManager does not work when the app is closed.

How do I set an alarm for a certain date on android?

Open the Clock app, and tap the down-arrow below the ON/OFF toggle on an alarm. In the expanded view, check "Repeat", and tap the circles for days you want the alarm to go off on.


1 Answers

You don't need to create Timestamps. You can do it with your Calendar.

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);

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

alarmManager.set(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(), pendingDinnerIntent);

I would also mention that as of KitKat, if your targetSdkVersion is 19 or above, the AlarmManager#set() method is not exact. If you want your alarm to fire at an exact time, you'll need to use a setExact*() method.

like image 171
Mike M. Avatar answered Nov 14 '22 04:11

Mike M.