我在使用应该对 AlarmManager 通知使用react的 BroadcastReceiver 时遇到了一些问题。我已经阅读了有关此主题的多个主题,但找不到任何有帮助的内容。我一定是错过了什么。
我按以下方式设置闹钟:
// Receiver intent
Intent intent = new Intent(mContext, CalendarAlarmReceiver.class);
intent.putExtra(CalendarAlarmReceiver.SHOW, show);
// Sender
PendingIntent sender = PendingIntent.getBroadcast(mContext, hashRequestCode(show), intent, PendingIntent.FLAG_UPDATE_CURRENT);
mPendingAlarmRequests.put(show.getChannel() + show.getName() + show.getStart().toGMTString(), sender);
// Get AlarmManager
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, show.getStart().getTime(), sender);
我创建了一个扩展 BroadcastReceiver 的 CalendarAlarmReceiver 类。在 onReceive() 方法中,我启动一个状态栏通知,如果应用程序正在运行,该通知可以正常工作。但是,如果我通过单击后退按钮直至其关闭来退出应用程序,则通知将不再出现。
我不确定接收者是否被调用,只是通知不起作用或者接收者是否没有被调用。
它已在 list 文件中注册:
<receiver android:name=".calendar.CalendarAlarmReceiver" >
</receiver>
我读到这可能不是接收器的故障,而是当应用程序停止时警报可能会被取消。如果是这种情况,是否可以简单地在后台始终运行的服务中设置警报来解决此问题?
最佳答案
警报由操作系统管理,当您的应用程序“退出”时通常不会清除。在接收器中添加一些日志记录并观察 logcat 输出以了解它是否被调用。如果是,请调试您的通知代码。
关于当用户退出应用程序时,Android AlarmManager BroadcastReceiver 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12682064/