AlertManager设置第一次闹铃响起后,间隔多长时间再响

时间:2022-02-28 17:00:15

第一次写博客,好激动。
用于记录自己的学习,有不对的地方,望指正。

在AlarmManager类中有这样一个方法:
AlarmManager aManager= (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
aManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),Long.parseLong(time), pIntent_multi);
用于设置闹铃第一次响铃后,间隔多长时间再次响起,我想把间隔时间和响铃时间的设置做到一个dialog中,这就需要重新写TimePickerDialog的布局,实现步骤:

1.继承TimePickerDialog
2.构造方法中重写布局
3.回调onTimeSet,但获取设置时间时。不能用其参数,切记切记。
上代码了,做下学习记录。

/*继承TimePickerDialog
*/
public class MyTimePickerDialog extends TimePickerDialog{
public MyTimePickerDialog(Context context,
OnTimeSetListener callBack, int hourOfDay, int minute,boolean is24HourView) {
super(context, callBack, hourOfDay, minute, is24HourView);
LayoutInflater inflater=LayoutInflater.from(context);
View view=inflater.inflate(R.layout.time_picker_dialog_my, null);
setView(view);
et_time=(EditText) view.findViewById(R.id.time_dis);
picker = (TimePicker)view.findViewById(R.id.timePicker);
picker.setIs24HourView(is24HourView);
picker.setCurrentHour(hourOfDay);
picker.setCurrentMinute(minute);
}
}
/点击设置按钮后,回调onTimeSet/
TimePickerDialog tDialog=new MyTimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
intent_multi=new Intent(MainActivity.this,AlarmReceiver.class);
pIntent_multi=PendingIntent.getBroadcast(MainActivity.this, 0, intent_multi, 0);
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
c.set(Calendar.MINUTE, picker.getCurrentMinute());
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0)
String time=et_time.getText().toString();
AlarmManager aManager= (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
aManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),Long.parseLong(time), pIntent_multi);
String text=picker.getCurrentHour()+” : “+format(picker.getCurrentMinute()+”“);
tv_set_multi.setText(text);
}
}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), is24);

        tDialog.setTitle(this.getResources().getString(R.string.set_time));
tDialog.show();

切记用自定义布局中timepicker的id来获取时间。