开发者

How to convert a list of dates into the AlarmManager?

开发者 https://www.devze.com 2023-03-30 19:48 出处:网络
I have a list of about 20 dates all formated like August 24,2011 How would i go about creatinga list of all of these dates and setting them to the alarm manager,

I have a list of about 20 dates all formated like

August 24,2011

How would i go about creating a list of all of these dates and setting them to the alarm manager,

So for example...开发者_如何学Go Today is the 25, if a date in the AlarmManager is set to 25, a notification is made.

How would i go about doing this?


Convert to a date first, using a formatter. Then get the timestamp from the date object using getTime(). Pass the timestamp to AlarmManager and specify the RTC or RTC_WAKEUP alarm type. Something like:

SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy");
Date date = sdf.parse(sdf);
long timestamp = date.getTime();

AlarmManager am = ...;
PendingIntent pi = ...;
am.set(AlarmManager.RTC, timestamp, pi);

Do keep in mind that, alarms are not persistent, so setting alarms too far in the future is not reliable: if the user reboots your phone (or kills your app/service), the alarms will be cleared. To register alarms on reboot, create a broadcast receiver for BOOT_COMPLETED.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号