Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlarmManager firing alarm past the time it was set on the same day, setRepeating

So basically I have this code, time returns 24hour time and repeats the alarm daily.

public setAlarm(String time, Context context){
    String[] strTime;

    strTime = time.split(":");

    int hour, min, sec;
    //set when to alarm
    hour = Integer.valueOf(strTime[0]);
    min = Integer.valueOf(strTime[1]);
    sec = 0;

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, min);
    cal.set(Calendar.SECOND, sec);
    //Create a new PendingIntent and add it to the AlarmManager
    Intent intent = new Intent(context, AlarmReceiverActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 19248, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}

Anyways, the problem is when I set the alarm 9am while the time is 9:10am, the alarm will go off. Why? I want it not to alarm if it is set past the system time. Ex. set the alarm at 9am while the system time is 9:10am

like image 837
useletters Avatar asked Jul 15 '13 03:07

useletters


People also ask

What is AlarmManager in Android?

android.app.AlarmManager. This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.

What is exact alarm?

Set an exact alarm. The system invokes an exact alarm at a precise moment in the future. If your app targets Android 12 or higher, you must declare one of the "Alarms & reminders" permissions; otherwise, a SecurityException occurs. Your app can set exact alarms using one of the following methods.

What is RTC alarm in Android?

Real-time clock (RTC) alarms are clock-based alarms that use Coordinated Universal Time (UTC). Only choose an RTC alarm in these types of situations: You need your alarm to fire at a particular time of day. The alarm time is dependent on current locale.


2 Answers

I got it working now. I added a checker of the alarm time and current time.

public setAlarm(String time, Context context){
        String[] strTime;

        strTime = time.split(":");

        int hour, min, sec;
        //set when to alarm
        hour = Integer.valueOf(strTime[0]);
        min = Integer.valueOf(strTime[1]);
        sec = 0;

        long _alarm = 0;
        Calendar now = Calendar.getInstance();
        Calendar alarm = Calendar.getInstance();
        alarm.set(Calendar.HOUR_OF_DAY, hour);
        alarm.set(Calendar.MINUTE, min);
        alarm.set(Calendar.SECOND, sec);

        if(alarm.getTimeInMillis() <= now.getTimeInMillis())
            _alarm = alarm.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
        else
            _alarm = alarm.getTimeInMillis();               

        //Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(context, AlarmReceiverActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 19248, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, _alarm, AlarmManager.INTERVAL_DAY, pendingIntent);
    }
like image 52
useletters Avatar answered Nov 07 '22 22:11

useletters


The accepted answer is wrong as I tried it and then figured out the solution.

It is wrong because, as you can see here: Calendar now = Calendar.getInstance(); Calendar alarm = Calendar.getInstance(); alarm.set(Calendar.HOUR_OF_DAY, hour); alarm.set(Calendar.MINUTE, min); alarm.set(Calendar.SECOND, sec);

"Calendar now" and "Calendar alarm" will always be the same no matter what because they are doing the same thing almost at the exact same spot in the code so the Calendar.getInstance() wil always be the same.

The solution is this

    long _alarm;
    Calendar now = Calendar.getInstance();
    long oldtimer = now.getTimeInMillis();

    cal.set(Calendar.HOUR_OF_DAY, Hours2int);
    cal.set(Calendar.MINUTE, minutes2int);
    cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
    cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
    cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

    //Calendar alarm = Calendar.getInstance();

    long newtimer = cal.getTimeInMillis();


    if(newtimer < oldtimer) {
        //do the thing
    }
like image 30
Cain Olivares Avatar answered Nov 07 '22 22:11

Cain Olivares