Search code examples
javaandroidalarmmanager

Android AlarmManager run every day 3 pm


I searched but didn't really find something. Just some examples for running AM every 20 seconds after hit a button etc. I want that on the first start of my app an AM is started which runs every day at 3 pm and checks some things. It should be possible that the AM is running everyday even if the app doesn't start for 2-3 days.

Can anyone help me?


Solution

  • Step #1: Create a Calendar object that represents 3pm tomorrow. Make sure that it is tomorrow, as if it is after 3pm today, just saying "3pm" will give you a time in the past.

    Step #2: Use that with AlarmManager. Use setRepeating() for Android 4.3 and below. For Android 4.4 and above, doing something at a precise time is frowned upon, and so you will probably wind up calling setExact() to get control once, then in that code call setExact() again to schedule the next event.

    Step #3: If you want to wake up the device when these events occur, use RTC_WAKEUP for the alarm type, and tie it to either a WakefulBroadcastReceiver or a regular BroadcastReceiver that uses my WakefulIntentService.