Android AlarmManager在错误的时间触发

编程入门 行业动态 更新时间:2024-10-20 08:43:15
本文介绍了Android AlarmManager在错误的时间触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的应用程序将设置AlarmManager触发某些事件,并在每天的12:00 pm重复该事件。 但是,我的客户报告说,警报有时会正常唤醒,但有时会在其他时间唤醒(例如10 / 11pm)。

My app will set an AlarmManager to fire some event and repeat it on 12:00pm everyday. However, my clients report that the alarm sometimes wake up normally, but sometimes wake up at other time (eg. 10/11pm).

代码段:

Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); if (calendar.get(Calendar.HOUR_OF_DAY) >= 12 && calendar.get(Calendar.MINUTE) > 0) { calendar.add(Calendar.DAY_OF_MONTH, 1); } calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.AM_PM, Calendar.PM); int interval = (60 * 60 * 24 * 1000); Intent intent = new Intent(mContext, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, AlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);

要取消警报:

Intent intent = new Intent(mContext, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, AlarmReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(pendingIntent);

启动应用程序/在一项活动中启用此设置时,我的应用程序将启动警报。因此,我使用FLAG_UPDATE_CURRENT来确保在特定时间仅执行一项任务。我的代码中是否缺少某些内容,导致在错误的时间触发了警报?

My app will start the alarm when the app is launch/turn on this setting in one of the activity. Therefore I use the FLAG_UPDATE_CURRENT to ensure only one task will fire at the specific time. Is there something missing in my code that result in firing the alarm at a wrong time?

推荐答案

我认为您可以在

Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 12); // 12 PM alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 60 * 60 * 24 * 1000, pendingIntent); // Daily interval, setting RTC_WAKEUP does exact time and forces device to wake up.

希望这会有所帮助。

更多推荐

Android AlarmManager在错误的时间触发

本文发布于:2023-11-27 07:27:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637126.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   时间   Android   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!