Android cannot pass intent extras though AlarmManager

孤人 提交于 2019-11-26 20:05:49
Vincent Hiribarren

I have some precisions that could help others, to be associated with the solution from Someone Somewhere. If you pass custom Parcelable objects as an extra, the operating system may not be able to process them, so an internal exception occurs and your extras are lost.

With Android N, even with PendingIntent.FLAG_UPDATE_CURRENT I cannot retrieve my custom Pacelable extras.

So I had to use system known Parcelable (like ParcelUuid) to reference some objects in a custom database instead of providing my whole Parcelable object.

Another solution is to convert Parcelable to a byte array that is correctly recognized by the system: How to marshall and unmarshall a Parcelable to a byte array with help of Parcel?

Someone Somewhere

UPDATE: Please see Vincent Hiribarren's solution


Old Answer... Haresh's code is not the complete answer... I used a Bundle and I tried without Bundle but I got null's either way when I attempting to obtain the strings from the extra's !!

The exact problem, in your code, is with the PendingIntent !

This is wrong if you're trying to pass extra's :

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, 0);

Because the 0 for the flags is what will cause you a headache

This is the right way to do it - specify a flag !

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

This is probably such a popular problem because Google's sample code neglected to include Extra's in an Alarm.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!