本应用具有如下功能:
新增学习计划,设置计划开始时间,结束时间。一起到达计划开始时间,系统会自动提醒。
批量删除学习计划
设置提醒方式,振动或声音或闪亮等
原理:
1 .在新加计划时,设置提醒时间,到时后转到AlarmBroadReceiver类,核心代码如下:
Intent intent = new Intent(context, AlarmReceiver.class);
Bundle bundle = new Bundle();
bundle.putString("info", planEntity.getContent());
bundle.putInt("id", planEntity.getId());
intent.putExtras(bundle);
//PendingIntent这个类用于处理即将发生的事情
通过getBroadcast第二个参数区分闹钟,将查询得到的ID值作为第二个参数。
PendingIntent pIntent = PendingIntent.getBroadcast(context, planEntity.getId(), intent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
Date selectedDate = DateUtils.parseString(planEntity.getStartDate(), "yyyy-MM-dd HH:mm");
am.set(AlarmManager.RTC_WAKEUP, selectedDate.getTime(), pIntent);
2.并将计划id和提醒时间保存到数据库,为了在开机重启,重新设置提醒
BootBroadcastReceiver
public class BootBroadcastReceiver extends BroadcastReceiver {
public static final LogContext LCTX = PlanLogContext.ROOT.lctx(BootBroadcastReceiver.class);
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
IPlanDao planDao=DaoFactory.getInstance().getDaoInstance(PlanDaoImpl.class);
List<AlarmEntity> result =planDao.queryAlarmRecords();
if(CollectionUtils.isEmpty(result)){
return;
}
for(AlarmEntity alarmEntity : result){
PlanEntity planEntity=planDao.queryPlanDataById(alarmEntity.getPlanId());
//重新记录闹钟
AlarmHelper.recordAlarm(context, planEntity);
LCTX.d("rerecord alarm . id = " + alarmEntity.getPlanId() +",alarmTime = "+ new Date(alarmEntity.getAlarmTime()));
}
}
}
}
3. 到提醒时间后,弹出notifictaion 信息
AlarmReceiver
ic void onReceive(Context context, Intent intent) {
// 获取传递的信息
Bundle bundle = intent.getExtras();
String info = bundle.getString("info");
int planId = bundle.getInt("id");
NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
int icon = R.drawable.icon; // 通知图标
CharSequence tickerText = info; // 状态栏显示的通知文本提示
long when = System.currentTimeMillis(); // 通知产生的时间,会在通知信息里显示
// 用上面的属性初始化Nofification
Notification notification = new Notification(icon, tickerText, when);
String selectedAlarmType = PreferencesUtils.getStringPreference(context, "config", "alarmType", "");
if (selectedAlarmType.equals(context.getString(R.string.alarmTye_sound))) {
//notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
}
else if (selectedAlarmType.equals(context.getString(R.string.alarmTye_vibrate))) {
//notification.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
}
else if (selectedAlarmType.equals(context.getString(R.string.alarmTye_light))) {
//notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}else{
notification.defaults = Notification.DEFAULT_SOUND;
}
// 设置提示框的状态
notification.flags = Notification.FLAG_AUTO_CANCEL; // //在通知栏上点击此通知后自动清除此通知
Intent intentTarget = new Intent(context, PlanMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, planId, intentTarget,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, "计划开始提醒" + planId, info, contentIntent);
notiManager.notify(planId, notification);
DaoFactory.getInstance().getDaoInstance(PlanDaoImpl.class).deleteAlarmRecord(planId);
界面: