Search code examples
androidalarmmanager

AlarmManager Android trigger alarm every day at 10 AM


I would like to run automatically, each day at predefined time (for. example at 10 AM) some code in my app.

I trying to do using the:

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 120 , pi);

But setRepeating accepts only time in miliseconds.

I would like to ask, is possible to set something like 10:00, REPEAT_DAILY ?

Thanks for any advice.


Solution

  • What about

    am.setRepeating(AlarmManager.RTC_WAKEUP, tenOclockToday,
            AlarmManager.INTERVAL_DAY, pi);
    

    You'll need to set long tenOclockToday equal to the millisecond value of 10:00 today (unless it is past 10:00, then you can set it to 10:00 tomorrow). You'll need to use a Calendar instance and set the time to 10:00, then get the milliseconds from the Calendar using calendar.getTimeInMillis()