Android编程设置提醒事件的方法
短信预约 -IT技能 免费直播动态提醒
本文实例讲述了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);
}
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);
}
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程序设计有所帮助。
您可能感兴趣的文章:Android实现消息提醒小红点效果Android高仿微信5.2.1主界面及消息提醒Android开发之使用通知栏显示提醒信息的方法详解Android中Notification通知提醒Android 开发之Dialog,Toast,Snackbar提醒Android消息个数提醒控件使用详解
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341