2013-05-21 79 views
1
Intent myIntent = new Intent(Alarm.this, Automaton.class); 
        myIntent.putExtra("methodName", "myMethod"); 
        myIntent.putExtra("hour", tp.getCurrentHour().toString()); 
        myIntent.putExtra("minute", tp.getCurrentMinute().toString()); 
        hour = tp.getCurrentHour(); 
        minute = tp.getCurrentMinute(); 
        PendingIntent pendingIntent = PendingIntent.getActivity(Alarm.this, 0, myIntent,PendingIntent.FLAG_UPDATE_CURRENT); 
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
         Calendar calendar = Calendar.getInstance(); 
         calendar.set(calendar.HOUR_OF_DAY, hour); 
         calendar.set(calendar.MINUTE, minute); 
         calendar.set(calendar.SECOND, 0); 
         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000, pendingIntent); 

以上是我的代碼。這是我的問題;我在另一個Activity中有一個方法,我想在指定的時間運行。如果用戶選擇了一天中還沒有通過的時間(比如你在早上10點設置鬧鐘時間爲上午11點,那麼它將工作正常)。但是,如果您在上午10點將鬧鐘設置爲9AM,則會立即觸發待定意向。不知道我哪裏錯了。任何幫助,將不勝感激!AlarmManager點火報警即時

回答

2

您可能需要檢查set time是否小於current time。如果設置的時間小於當前時間,請不要爲其設置鬧鐘。因爲即使在時間已逝的情況下保留它也沒用。

+0

謝謝你的快速反應APPU,可以說我現在要設置鬧鐘(下午11:45)至明天5:30 AM運行。從技術上講,上午5點半已經過去了,所以如果我去設置它,它會立即啓動。這就是我想要避免 – user2121927

+0

不,我可以說你明白事情是錯誤的。因爲鬧鐘應該在毫秒的幫助下設置。因此,現在以毫秒爲單位的11.45PM技術上明天小於5:30 AM(以毫秒爲單位) – Kanth

2

您可以取日曆實例並將日期設置爲第二天並將其用於警報。

0

對於@shreyas說,你需要檢查自己,如果你需要碰到一天。

示例..如果他們選擇5:00 PM(HOUR_OF_DAY = 17),將其與當前時間進行比較。如果該時間(17)小於當前時間,則在您的日曆實例中添加一天。

int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); 
calendar.set(calendar.DAY_OF_WEEK, dayOfWeek + 1);