我有一个程序,允许用户输入 3 次警报,然后在适当的时间显示警报。但是,每当我设置警报时,只会执行最后一个警报。
所以基本上,用户输入 1,2 或 3 然后我执行时间选择对话框的数量,然后将这些时间发送到警报接收器。但只有最后一个警报正在执行。任何帮助表示赞赏。为代码长度道歉,我知道这非常低效。
警报活动片段::
buttonSetTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(alarmNumInt == 1){
tvSet.setText("");
openTimePicker1(false);
}
else if (alarmNumInt == 2){
tvSet2.setText("");
openTimePicker2(false);
tvSet.setText("");
openTimePicker1(false);
}
else {
tvSet3.setText("");
openTimePicker3(false);
tvSet2.setText("");
openTimePicker2(false);
tvSet.setText("");
openTimePicker1(false);
}
}
});
protected void openTimePicker1(boolean b) {
// TODO Auto-generated method stub
Calendar calendar = Calendar.getInstance();
timePromptDia = new TimePickerDialog(
AlarmActivity.this,
onTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
b);
timePromptDia.setTitle("Set Alarm 1 Time");
timePromptDia.show();
}
protected void openTimePicker2(boolean b) {
// TODO Auto-generated method stub
Calendar calendar = Calendar.getInstance();
timePromptDia2 = new TimePickerDialog(
AlarmActivity.this,
onTimeSetListener2,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
b);
timePromptDia2.setTitle("Set Alarm 2 Time");
timePromptDia2.show();
}
protected void openTimePicker3(boolean b) {
// TODO Auto-generated method stub
Calendar calendar = Calendar.getInstance();
timePromptDia3 = new TimePickerDialog(
AlarmActivity.this,
onTimeSetListener3,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
b);
timePromptDia3.setTitle("Set Alarm 3 Time");
timePromptDia3.show();
}
OnTimeSetListener onTimeSetListener = new OnTimeSetListener(){
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm(calSet);
//condition for other alarms
}};
OnTimeSetListener onTimeSetListener2 = new OnTimeSetListener(){
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm2(calSet);
}};
OnTimeSetListener onTimeSetListener3 = new OnTimeSetListener(){
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm3(calSet);
}};
protected void setAlarm(Calendar targetCal) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getBaseContext(), req, intent1, 0);
AlarmManager alarmManager1 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager1.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent1);
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,
targetCal.getTimeInMillis(),
TimeUnit.HOURS.toMillis(24),
pendingIntent1);
tvSet.setText("Alarm 1 is set @ " + targetCal.getTime() + "\n" +" Repeat everyday\n");
}
protected void setAlarm2(Calendar targetCal2) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getBaseContext(), req2, intent2, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal2.getTimeInMillis(), pendingIntent2);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
targetCal2.getTimeInMillis(),
TimeUnit.HOURS.toMillis(24),
pendingIntent2);
tvSet2.setText("Alarm 2 is set @ " + targetCal2.getTime()+" Repeat everyday\n");
}
protected void setAlarm3(Calendar targetCal3) {
// TODO Auto-generated method stub
Intent intent3 = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent3 = PendingIntent.getBroadcast(getBaseContext(), req3, intent3, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal3.getTimeInMillis(), pendingIntent3);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
targetCal3.getTimeInMillis(),
TimeUnit.HOURS.toMillis(24),
pendingIntent3);
tvSet3.setText("Alarm 3 is set @ " + targetCal3.getTime() + " Repeat everyday\n");
}
}
和广播接收器活动:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
playSound();
Toast toast;
toast = Toast.makeText(arg0, "Time for Blood Glucose Testing!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0,0);
toast.show();
Vibrator vibrator = (Vibrator) arg0.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
}
public void playSound(){
ToneGenerator alertTone = new ToneGenerator(AudioManager.STREAM_DTMF,ToneGenerator.MAX_VOLUME);
alertTone.startTone(ToneGenerator.TONE_DTMF_1, 1000);
}
}