1

所以我在我的应用程序从中获取的数据库中设置了警报,但是我希望能够在这些警报上设置到期日期(用于药物治疗)。我该怎么做?

这是我的警报代码:

private void calMeds(int medication1, String time1) {

    String string = time1; 
    String[] parts = string.split(":");
    String part1 = parts[0]; 
    String part2 = parts[1]; 
    Log.e("DATABASE", "Hours: " + part1);
    Log.e("DATABASE", "Minutes: " + part2);

    hourCorrect = Integer.parseInt(part1);
    minuteCorrect = Integer.parseInt(part2);

    Date date = new Date();
    Calendar cal_Alarm = Calendar.getInstance();
    Calendar cal_Now = Calendar.getInstance();
    cal_Now.setTime(date);

    cal_Alarm.setTime(date);
    cal_Alarm.set(Calendar.HOUR_OF_DAY, hourCorrect);
    cal_Alarm.set(Calendar.MINUTE, minuteCorrect);
    cal_Alarm.set(Calendar.SECOND, 3);

    if (cal_Alarm.before(cal_Now)) { 
        cal_Alarm.add(Calendar.DATE, 1);

    }

    alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    String ALARM_ACTION;

如果我每天都有一个闹钟在 9:00 响起,并且它的到期日期是 3 月 21 日,我该如何阻止它在此日期之后发出警报?这些时间被输入数据库,每个都有不同的到期日期。

4

2 回答 2

0

使用调度程序来安排闹钟的到期时间

示例:-(假设您想在 30 分钟后退出程序)

 java.util.Timer timer = new java.util.Timer();
  timer.schedule(new TimerTask() {

                        @Override
                        public void run() {
                            System.exit(0);
                        }
                    }, 30 * 60 * 1000);
                }
于 2013-03-19T18:03:26.383 回答
0

根据您的修订:

Calendar now = Calendar.getInstance();
    Calendar then = Calendar.getInstance();
    then.set(Calendar.MONTH, month);
    then.set(Calendar.DAY_OF_MONTH, day);
    then.set(Calendar.HOUR, hour);
    then.set(Calendar.MINUTE, minute);

    long dif = then.getTimeInMillis() - now.getTimeInMillis();

    ActionListener action = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if(!alarm.isStopped())
            alarm.stop();
    }
    };

    Timer timer = new Timer((int)dif, action);
    timer.start();
于 2013-03-19T18:08:02.910 回答