How To Set Alarm To Fire Everyday At 8:00am
Solution 1:
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,000
You 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:
Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
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
So how would that look inside AlarmManager.setRepeating()?
Here is the method:
setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
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
triggerAtTime = cal.getTimeInMillis()-Calendar.getInstance().getTimeInMillis()
Then it would be
alarmMan.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime, 86400000, pendingIntent);
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()
, then cal.add(Calendar.DAY, 1)
then cal.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.
Post a Comment for "How To Set Alarm To Fire Everyday At 8:00am"