tags:

views:

325

answers:

1

Hi Folks

I use a custom Parcelable to carry some data to a BroadcastReceiver. Here is what i do:

I register my intent and set the extra Parcelable on it along with an extra classloader (intent.setExtraClassLoader(..)). Next i schedule the execution of the broadcast via an AlarmManager.

So when the AlarmManager fires it looks at my intent with its parcel which it can not process since it doesn't use the supplied classloader (as it seams).

I think the classloader gets lost when Inten.fillIn copys the intent to a new one (see stack trace).

02-21 21:09:25.214: WARN/Intent(52): android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.company.project.MyParcelable 02-21 21:09:25.214: WARN/Intent(52): at android.os.Parcel.readParcelable(Parcel.java:1822) 02-21 21:09:25.214: WARN/Intent(52): at android.os.Parcel.readValue(Parcel.java:1713) 02-21 21:09:25.214: WARN/Intent(52): at android.os.Parcel.readMapInternal(Parcel.java:1947) 02-21 21:09:25.214: WARN/Intent(52): at android.os.Bundle.unparcel(Bundle.java:169) 02-21 21:09:25.214: WARN/Intent(52): at android.os.Bundle.putAll(Bundle.java:242) 02-21 21:09:25.214: WARN/Intent(52): at android.content.Intent.fillIn(Intent.java:4530) 02-21 21:09:25.214: WARN/Intent(52): at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:185) 02-21 21:09:25.214: WARN/Intent(52): at android.app.PendingIntent.send(PendingIntent.java:400) 02-21 21:09:25.214: WARN/Intent(52): at com.android.server.AlarmManagerService$AlarmThread.run(AlarmManagerService.java:636)

So is there any way around that problem? Any help would be appreciated.

Thanks

+3  A: 

Put com.company.project.MyParcelable in the actual application, instead of doing whatever games you are playing with classloaders. Then, it should be available from both the sender and recipient of the Intent.

CommonsWare
The AlarmManager is scheduled when a BOOT_COMPLETED action is received. So i don't start the intent in my application.Is there a better way to handle the situation?
Soulreaper
Oh! I see. I didn't notice that in the stack trace. Off the cuff, it would appear you cannot use a custom `Parcelable` with a `PendingIntent`, which kinda stinks. Rather than use `Parcelable`, you might need to serialize your object some other way (e.g., to a `Bundle`, to a `String`).
CommonsWare
Yeah piping the data through an extra ready object is one way to go. The strange thing though is, that i get the mentioned exception but my data object is actually transfered. So it does work but spills out the exception all the time which is confusing and threatening.
Soulreaper
Hmmmm...you might want to post this as an issue out on http://b.android.com, if there isn't already an issue for it. I can't for the life of me figure out why `PendingIntent` is trying to deserialize your `Parcelable`.
CommonsWare
I had the same issue with a custom Parcelable object. I solved it using another serialization method (XStream), which I now use only for passing objects between activities.
Silvio Donnini