gpt4 book ai didi

android - alarmmanager 立即启动

转载 作者:行者123 更新时间:2023-11-30 00:13:24 25 4
gpt4 key购买 nike

我想在每周的午夜设置闹钟这是我的代码:

int notificationId = getNotificationId(); //it get a random number
Context context = rule.context;
Intent intent = ((Activity) context).getIntent();
long time = getRuleCalendar().getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time,pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, time,pendingIntent);
}

public calendar getRuleCalendar(){
Calendar calSet = Calendar.getInstance();
calSet.set(Calendar.DAY_OF_WEEK, calendarDay); //calendarDay change by day of weeks
calSet.set(Calendar.HOUR_OF_DAY, 0);
calSet.set(Calendar.MINUTE, 0);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 1);
return calSet
}

现在我的问题是一个闹钟会立即启动(现在是午夜过后,对我来说没问题,我想今天检查一下),但所有其他闹钟都在错误的时间启动。为什么?

最佳答案

只有在您过去设置闹钟时,闹钟才会立即触发。例如,在您的情况下,您正在为今天的 00:00 AM 设置闹钟,这始终是今天的过去日期。所以你已经设置了第二天 00:00 AM 的闹钟。为此,只需在 Calendar.DAY_OF_YEAR 中添加 1。

 public Calendar getRuleCalendar(){
Calendar calSet = Calendar.getInstance();
calSet.set(Calendar.HOUR_OF_DAY, 0);
calSet.set(Calendar.MINUTE, 0);
calSet.set(Calendar.SECOND, 0);
calSet.add(Calendar.DAY_OF_YEAR,1);
calSet.set(Calendar.MILLISECOND, 1);
return calSet;
}

以及设置重复警报你必须检查Documentation .

如果您不知道从 android M 开始的 Doze 模式 中的背景行为变化。那么请查看 Optimizing for Doze and App Standby .

关于android - alarmmanager 立即启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47754793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com