Android编程设置提醒事件的方法
程序员文章站
2023-12-21 11:14:58
本文实例讲述了android编程设置提醒事件的方法。分享给大家供大家参考,具体如下:
1、启动service
intent intent = new inten...
本文实例讲述了android编程设置提醒事件的方法。分享给大家供大家参考,具体如下:
1、启动service
intent intent = new intent(this,autotaskservice.class); intent.putextra("reminder_event", remindermodel); startservice(intent);
2、service file
public class autotaskservice extends service { private remindermodel mremindermodel = null; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { super.oncreate(); } @override public void ondestroy() { super.ondestroy(); } @override public void onstart(intent intent, int startid) { if(intent == null) return; if(intent.hasextra("reminder_event")) { mremindermodel = (remindermodel) intent.getserializableextra("reminder_event"); } setreminderevent(); super.onstart(intent, startid); } /** * set reminder event */ private void setreminderevent() { if(mremindermodel == null) return; if(textutils.isempty(mremindermodel.reminderstarttime)) return; if(textutils.isempty(mremindermodel.remindertime)) return; calendar cal = getcalendarfromdate(mremindermodel.reminderstarttime); string[] array = mremindermodel.remindertime.split("-"); for(int i = 0; i < array.length; i++) { if(i == 0) { cal.set(calendar.hour_of_day, integer.parseint(array[0])); } else if (i == 1) { cal.set(calendar.minute, integer.parseint(array[1])); } } cal.set(calendar.second, 0); intent intent = new intent(autotaskservice.this, alarmreceiver.class); if(!textutils.isempty(mremindermodel.reminderpath)) { intent.putextra("reminder_pic_path", mremindermodel.reminderpath); } pendingintent pi = pendingintent.getbroadcast(this, 0, intent, pendingintent.flag_cancel_current); // 获取alarmmanager对象 alarmmanager am = (alarmmanager) getsystemservice(alarm_service); // alarmmanager.rtc_wakeup休眠时会运行,如果是alarmmanager.rtc,在休眠时不会运行 am.set(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), pi); } /** * @param date format is 2012-9-18 * @return calendar value is after set date's value */ private calendar getcalendarfromdate(final string date) { int year = 0; int month = 0; int day = 0; try { string[] array = date.split("-"); int[] arrayint = new int[array.length]; for (int i = 0; i < array.length; i++) { arrayint[i] = integer.parseint(array[i]); if(i == 0) { year = arrayint[0]; } else if(i == 1){ month = arrayint[1]; } else if(i == 2){ day = arrayint[2]; } } } catch (exception e) { e.printstacktrace(); } calendar cal = calendar.getinstance(); if(year > 0 && month >= 0 && day >= 0) { cal.set(year, month - 1, day); } return cal; } }
3、定时接收器
public class alarmreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { string path = null; if(intent.hasextra("reminder_pic_path")) { path = intent.getstringextra("reminder_pic_path"); } log.i("======> alarmreceiver", path); // 启动通知activity intent it = new intent(context, fingerpaint.class); it.addflags(intent.flag_activity_new_task); if(!textutils.isempty(path)) { it.putextra("reminder_pic_path", path); } context.startactivity(it); } }
希望本文所述对大家android程序设计有所帮助。