2013-03-19 90 views
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去滅,到期日是三月21日,我該如何阻止它從後這一令人震驚日期?時間被輸入到數據庫中,並且每個都有不同的到期日期。

回答

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

我想這個問題被誤解了,我會編輯它.. – una 2013-03-19 18:58:29

0

使用調度程序安排報警的到期

例子: - (假設你想30分鐘後,退出你的程序)根據您的修改

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

         @Override 
         public void run() { 
          System.exit(0); 
         } 
        }, 30 * 60 * 1000); 
       }