gpt4 book ai didi

java - 设置警报到期时间

转载 作者:行者123 更新时间:2023-12-01 04:47:59 25 4
gpt4 key购买 nike

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

这是我的警报代码:

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 日,我该如何阻止它在此日期之后响铃?这些时间被输入到数据库中,并且每个时间都有不同的到期日期。

最佳答案

根据您的修订:

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();

关于java - 设置警报到期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com