簡體   English   中英

當鬧鍾時間與實時時間相距1小時的倍數時,會觸發PendingIntent

[英]PendingIntent fires when the alarm time is a multiple of 1 hour away from real time - Android

我正在創建一個鬧鍾應用程序。 當達到使用AlarmManager設置的時間時,可以設置PendingIntent ,取消它們並使用我的BroadcastReceiver接收它們。 我發現了最近的問題。

以前,我可以在將來的任何時候設置警報,並且BroadcastReceiver直到到達該時間才可以“接收” PendingIntent。 我想我從未涉及過要設置的警報正好在1小時或更長時間(僅限整數)的情況。 例如,當前時間是11:54,我將警報設置為12:54或1:54、2:54等。當我這樣做時,BroadcastReceiver會收到PendingIntent並執行我告訴它的操作去做。

為什么會這樣? 當我將分鍾更改為其他內容時,只有當分鍾與應用程序的行為相同,就像我將鬧鍾設置為當前時間一樣時,它才不會發生。

這是我設置警報的方式:

public void scheduleAlarm(Context aContext) {
    AlarmManager am = (AlarmManager) aContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(aContext, MyBroadcastReceiver.class);

    String id = this.getId().replaceAll("[^0-9]+", "");      // this.getId returns a string such as "alarm1". We only need the "1".

    PendingIntent alarmIntent = PendingIntent.getBroadcast(aContext, Integer.parseInt(id), intent, 0);

    // "this" in this context is the Alarm object. So you can get the hour and minute from the timepicker used to set the alarm
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, this.getHour());
    calendar.set(Calendar.MINUTE, this.getMinute());

    long calendarTime = calendar.getTimeInMillis();
    am.setExact(AlarmManager.RTC_WAKEUP, calendarTime, alarmIntent);
}

我檢查了設置鬧鍾的時間,並確認它們不是當前時間,因此不應關閉。 嘗試設置多個警報並發現問題仍然存在后,我從手機上卸載了該應用程序,並進行了全新安裝。 之后,一切正常,並且仍然可以正常運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM