android - Alarmmanager setExact 方法无法正常工作
问题描述
Hı 我正在使用 Alarmmanager 来运行我的广播接收器代码,但是当添加过去时间的日历时(例如,如果当前时间是 15:32并且我
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 7);
calendar.set(Calendar.SECOND,23);
使用上面的代码,我的广播接收器调用每个 setupAlarm 方法,如果我
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 7);
calendar.set(Calendar.SECOND,23);
使用这一切都很好,有人知道原因吗?
` private void setupAlarm(Context context) {
Intent myIntent = new Intent(context, MyBroadcastReceiver.class);
PendingIntent myPendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 7);
calendar.set(Calendar.SECOND,23);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmMgr != null) {
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), myPendingIntent);
}
}`
解决方案
推荐阅读
- c# - 使用 Excel COM 创建后关闭 Excel 进程
- logstash - Logstash 解析 json
- java - 为什么当我点击按钮时我的数据不去也不提交到 MySQL 数据库?
- spring - 了解 CrudRepository 保存/更新
- angular - 一个组件中的多个页面
- python - 在 Python 中,如何将数值数组列表转换为简单的一维 numpy 数组?
- c# - 基于另一个属性的 C# 属性
- .htaccess - Magento 2.4.2 在 apache 服务器中显示 404 页面
- javascript - 当用户点击使用时刻时显示下一个明天的日期或昨天的日期
- javascript - OnSubmit 函数不会更新 React 中的反应钩子状态变量