AlarmManager将时间换班12小时

时间:2017-12-29 20:35:39

标签: android alarmmanager

我试图创建定期通知。 所以,我创建了一个重新安排新通知时间的函数:

private void rescheduleNotification(Context context) {
    long nextNotifTime = System.currentTimeMillis();
    // Schedule next notification in 15 minutes
    nextNotifTime += 15 * 60 * 1000;

    Calendar calendar = new GregorianCalendar();
    calendar.setTimeInMillis(nextNotifTime);
    // It's an old version with the same result
    //calendar.add(Calendar.MINUTE, 15);

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm:ss", Locale.ENGLISH);
    this.logEvent("      Next notification time is: " + sdf.format(calendar.getTimeInMillis()));

    Intent intent = new Intent(context, WordsBcReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager manager = (AlarmManager)context.getSystemService(ALARM_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= 23) {
        manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextNotifTime, pendingIntent);
    } else {
        manager.set(AlarmManager.RTC_WAKEUP, nextNotifTime, pendingIntent);
    }
    this.logEvent("   Set next notification time: " + sdf.format(nextNotifTime));
}

有时它运行正常,但有时通知时间恰好会移动12小时。

我添加了特殊功能来记录所有时间操作,因此日志包含:

29/12/17 11:30:36:    Next notification time is: 29/12/17 12:00:36
29/12/17 11:30:36:    Set next notification time: 29/12/17 12:00:36

似乎没问题,但 adb shell dumpsys alarm 表示:

type=0 whenElapsed=+11h34m13s906ms when=2017-12-30 00:00:36

我尝试使用日历(我稍后会排除某些时段),但结果是一样的。 无法找到问题...

0 个答案:

没有答案