I am trying to set an alarm to fire everyday at 8:00am.
I know how to create the alarm, but how will i set it to launch everyday at 8:00am.
am.setRepeating()
I am trying to set an alarm to fire everyday at 8:00am.
I know how to create the alarm, but how will i set it to launch everyday at 8:00am.
am.setRepeating()
You could use Calendar and set it for the appropriate time that you want. Then you would do
cal.getTimeInMillis()
, and use that for the triggerAtTime, and the interval would be 24*60*60*1000 = 86,400,000You would have to also make sure you have a BroadcastReceiver for boot completed, so if the phone is powered off then back on, you can re-schedule the alarm:
For boot, you use intent-filter
"android.intent.action.BOOT_COMPLETED"
and you must hold the permission"android.permission.RECEIVE_BOOT_COMPLETED"
, just in case you needed that info.For your convenience, here are a couple links. The page on Calendar:
http://developer.android.com/reference/java/util/Calendar.html
And the page on AlarmManager:
http://developer.android.com/reference/android/app/AlarmManager.html
Here is the method:
And I guess for type, you would want to use ELAPSED_REALTIME, then to get triggerAtTime, you would get a Calendar (call it cal) that matched 8:00 AM tomorrow morning, then do
Then it would be
And I don't know how exactly to get tommorrow at 8:00 AM using Calendar, but I'm thinking you would do
cal.getInstance()
, thencal.add(Calendar.DAY, 1)
thencal.set(Calendar.HOUR_OF_DAY, 8)
http://developer.android.com/reference/java/util/Calendar.html
I have hardly used Calendar, so I may have some errors, and you may have to play with it a little, but that's essentially what would need to be done. In the future, if you just read the DOCs and play with it some, you'll usually be able to figure it out.