Android Bundle.containsKey 抛出异常
我正在编写一个 Android 应用程序。在此应用程序中,用户将来应该能够收到通知。我正在使用 AlarmManager 将广播发送到我的接收器,该接收器内部有一个意图,通知作为额外的包裹。我不想将整个代码放在这里(太多了),所以我只是解释工作流程并将相关代码放在这里。
我已经扩展了通知,让它自己拥有一个 id 和一个来自我的模型的对象(模型是可打包的)。我注意通知在我的模型之前被打包和取消打包。
要发送警报,我将创建一个新的通知实例并使用模型中的数据调用 setLatestEventInfo
。如果我直接推送这个通知,一切都很好。
之后,我创建一个新的待广播意图。放入广播中的 Intent 将我的通知作为 ParcelableExtra 获取。然后我通过 AlarmManager.set() 触发 PendingIntent。如果时间到了,我会在广播接收器中收到广播。
我从我的 Intent 中获取额外内容,并想查看一下,那里有一个我用来存储它的“NotificationExtra”键。在该调用中我得到一个 ClassCastException。如果您对某些代码示例感兴趣,请随时询问。知道会出什么问题吗?我可以将 null 值存储为 Parcelables 吗?如果我可以存储空值,它们可以被正确读取吗?
I'm writing an Android Application. In this App the user should be able to recieve Notifications in the future. I'm using AlarmManager to send a Broadcast to my Reciever which has an Intent inside with the Notification as Parceled Extra. I don't want to put the whole Code here (its much), so I'm explaining just the workflow and put relevant code here.
I have extended Notification to let itself have an id and an Object from my Model (Model is Parcelable). I take care that the Notification gets Parcelled and unparcelled before my Model.
To send an Alarm I would create a new Notification Instance and call setLatestEventInfo
with data from the Model. If I push this Notification directly, everything is fine.
After that, I create a new Pending-Broadcast-Intent. the Intent, which is put inside the Broadcast gets my Notification as ParcelableExtra. Then I fire that PendingIntent Via AlarmManager.set(). if time has come, I recieve the Broadcast in my BroadcastReciever.
I take the extras from my Intent and want to look, id theres a Key "NotificationExtra" which I used to store it. in that call I get an ClassCastException. If you are intereseted in some Code samples, feel free to ask. Any Idea what can go wrong? can I store null Values as Parcelables? And If I can store null values, can they be read properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论