android - Android Calendar.getInstance() 在午夜后没有得到今天
问题描述
我有以下代码:
Calendar nextSchedule = Calendar.getInstance();
nextSchedule.set(Calendar.HOUR_OF_DAY, 0);
nextSchedule.set(Calendar.MINUTE, 0);
nextSchedule.set(Calendar.SECOND, 10);
nextSchedule.set(Calendar.MILLISECOND, 0);
nextSchedule.add(Calendar.DAY_OF_YEAR, 1);
Calendar cal = Calendar.getInstance();
long diff = nextSchedule.getTimeInMillis() - cal.getTimeInMillis();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
正如预期的那样,在上午 11 点左右,它给了我 13/12 小时,但是在小部件中执行以下操作时:
Calendar nextSchedule = Calendar.getInstance();
nextSchedule.set(Calendar.HOUR_OF_DAY, 0);
nextSchedule.set(Calendar.MINUTE, 0);
nextSchedule.set(Calendar.SECOND, 10);
nextSchedule.set(Calendar.MILLISECOND, 0);
nextSchedule.add(Calendar.DAY_OF_YEAR, 1);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextSchedule.getTimeInMillis(), pendingIntent);
凌晨 1 点 30 分左右,它仍然没有更新。只有在凌晨 2 点到 9 点之间(我睡着了)它才更新。
类似的奇怪事情发生在其他地方。我有一个带有以下代码的日历视图(applandeo):cvCalendar.setOnDayClickListener(eventDay -> showDateItems(eventDay,lvCalendar));
private void showDateItems(EventDay eventDay, ListView lvCalendar) {
Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal.set(eventDay.getCalendar().get(Calendar.YEAR),eventDay.getCalendar().get(Calendar.MONTH),eventDay.getCalendar().get(Calendar.DATE));
cal2.set(eventDay.getCalendar().get(Calendar.YEAR),eventDay.getCalendar().get(Calendar.MONTH),1);
cal2.add(Calendar.MONTH, -1);
ArrayList<String> items = new ArrayList<>();
String line;
for (ItemsHistoryItem item: Utils.getInstance().itemsHistory) {
if (item.getxDay() < Utils.getInstance().calenderToEpoch(cal2) && item.getyDay() < Utils.getInstance().calenderToEpoch(cal2)) break;
if (item.getxDay() != Utils.getInstance().calenderToEpoch(cal) && item.getyDay() != Utils.getInstance().calenderToEpoch(cal)) continue;
line = item.getItemId() + ": $" + item.getAmount();
if (item.getxDay() == Utils.getInstance().calenderToEpoch(cal))
line += " (x day)";
else
line += " (y day)";
items.add(line);
}
ArrayAdapter<String> lcAdapter = new ArrayAdapter<>(requireActivity(), android.R.layout.simple_list_item_1, items);
lvCalendar.setAdapter(lcAdapter);
}
long calenderToEpoch(Calendar cal2) {
return TimeUnit.MILLISECONDS.toDays(cal2.getTimeInMillis());
}
基本上,上面的代码扫描项目的日期,当用户选择某个日期时,他会得到一个项目列表,其中日期(x 和 y)属于该日期。白天这样做时,它会起作用。当在凌晨 1:30 这样做时(例如),它会提前一天移动,所以如果用户点击 12-oct,他会得到 11-oct 的项目。在凌晨 2 点到 9 点之间,它会“转移”回来并按预期工作。
知道这里有什么问题吗?
解决方案
如果您希望它在您想要的时间准确触发,我有一个正确使用警报管理器的建议,否则当设备处于深度睡眠模式并延迟时它不会按时触发:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// use setAlarmClock function of AlarmManager
// but this function will show an alarm icon on statusbar
// if you dont want to show that icon you can use
// setExactAndAllowWhileIdle function but that will not be
// perfectly exact
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// use setExact function of AlarmManager
}
else {
// use set function of AlarmManager
}
推荐阅读
- javascript - 随机化字符串中的每个字符大小写
- flutter - 控制 Flutter ListView 的滚动
- flutter - 是否可以只需要 Dart 中的两个参数之一?
- c# - 如何在 Visual Studio 的每个类方法中插入打印语句
- google-apps-script - 谷歌应用脚本触发器不发送电子邮件
- swift - 斯威夫特 | Switch 语句:等到案例完成后再继续下一个案例
- mockito - 带有 JUnit 5 的 Powermock 或 PowerMockito
- spacy - 如何获取未发布的 spaCy 版本 (v3) 的模型?
- corda - 在 VaultCustomQuery 中获取帐户名称
- android - 如何在 Android Studio 中的 ListView 中显示 Html 列表