2016-09-14 158 views
2

我從很長時間以來一直在尋找這個要求,並且找不到任何東西。所以我正在尋求你們的幫助。如何在特定日期範圍內設置鬧鐘?

我有一個需求,我需要在特定日期範圍內設置鬧鐘管理器。我的意思是從2016年9月28日到2016年9月30日,具體時間是13:00。

目前我可以設置特定的日子。但不適用於日期範圍。我的電流如下。

AlarmManager objAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);    
     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.YEAR, 2016); 
     calendar.set(Calendar.MONTH, 8); 
     calendar.set(Calendar.DAY_OF_MONTH, 28); 
     calendar.set(Calendar.HOUR_OF_DAY, 13); 
     calendar.set(Calendar.MINUTE, 00); 
     calendar.set(Calendar.SECOND, 0); 
     calendar.set(Calendar.MILLISECOND, 0); 
     calendar.set(Calendar.AM_PM, Calendar.AM);   

     Intent alamShowIntent = new Intent(this,AlarmActivity.class); 
     PendingIntent alarmPendingIntent = PendingIntent.getActivity(this, 0,alamShowIntent,0); 

     objAlarmManager.set(AlarmManager.RTC_WAKEUP,objCalendar.getTimeInMillis(), alarmPendingIntent); 

請問您可以幫我解決這個問題。

想到的一個想法是找到日期範圍之間的增量,並在該特定時間重複鬧鐘。這是正確的方法嗎?

問候, 沙拉斯

回答

0
  1. 時間表2016年9月28日在13.00h第一個報警。
  2. 在警報啓動時運行的代碼中,重新安排警報。使用當前日期並添加24小時,以便明天同時啓動。
  3. 做第2步除非日期過去9月30日。警報不會重新計劃,不會再發射。

這種方法需要你使用重複報警像setRepeating()。但是,如果您正在開發Android 6.0+,那麼這很方便,因爲重複鬧鐘不會在睡眠模式下觸發。

0

在設置新警報時,您無法通過該警報提供到期的警報。 我會建議把你的報警到期日期的意圖,即alamShowIntent &使用setRepeating()報警所需的時間。在每一次鬧鈴事件中,都要檢查結束日期是否應該大於鬧鈴時間。如果是,則執行任務&如果不是,則使用相同的PendingIntent取消警報。

相關問題