最后一部分是提醒以及每天任务刷新,两者都用到了
AlarmManager
这个系统管理类。
提醒
提醒功能就是一个闹钟的效果,只是这里是启动服务,在服务里发一条notification作为提醒。设置时间时,
TimePicker控件会监听该事件,每次时间改变后都会重新设置系统闹钟;如果用户关闭提醒功能的话,这个系统闹钟设置的事件也会取消。修改后的
SettingFragment如下:
<span style="font-size:24px;">@EFragment (R.layout.frag_setting )
public class SettingFragment extends BaseFragment {
@ColorRes(R.color. setting_text_dark)
int alarmColor;
@ColorRes(R.color. main_bg)
int remindColor;
@ViewById
ToggleButton tb_alarm;
@ViewById
TimePicker tp_alarm;
@Pref
GlobalPrefBean_ globalPrefBean;
private boolean initViewFinished;
@AfterViews
void AfterViews() {
initViewFinished = true;
tp_alarm.setIs24HourView( true);
Util. setNumberPickerTextSizeAndColor( tp_alarm, 25, alarmColor);
Util. resizeTimerPicker( tp_alarm, 100, LayoutParams.WRAP_CONTENT );
tp_alarm.setOnTimeChangedListener( new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hour, int min) {
onTimeSet(hour, min);
}
});
initView();
initPendingIntent();
}
@Override
public void show() {
if ( initViewFinished) {
initView();
}
}
void initView() {
String alarmTime = globalPrefBean .alarmTime().getOr("" );
if ( "".equals(alarmTime)) {
tp_alarm.setEnabled( false);
tb_alarm.setChecked( true);
tp_alarm.setCurrentHour(0);
tp_alarm.setCurrentMinute(0);
tb_alarm.setTextColor( remindColor);
} else {
int hour = Integer.parseInt(alarmTime.subSequence(0, 2).toString());
int min = Integer.parseInt(alarmTime.subSequence(2, 4).toString());
tp_alarm.setEnabled( true);
tb_alarm.setChecked( false);
tp_alarm.setCurrentHour(hour);
tp_alarm.setCurrentMinute(min);
tb_alarm.setTextColor( alarmColor);
setSystemAlarm(hour, min);
}
}
@Click
void tb_alarm() {