Skip to content Skip to sidebar Skip to footer

Sending Serializable Object Between Applications With Broadcast Receiver

Im trying to send an Object that implements 'Serializable' from one application to another via a Broadcast receiver. I want to send a class like: public class MyObject implements S

Solution 1:

Im trying to send an Object that implements "Serializable" from one application to another via a Broadcast receiver.

That is not a good idea. It requires both apps to have the same class definition for that Java class, or at least one that is compatible. Since the apps may not be updated at the same time, you can run into cases where the sender has a newer edition of the Java class than does the recipient, which can cause a deserialization exception.

In application A i do this:

That is not a good idea. You are not sending the data to the other app. You are sending the data to any app that cares to listen for this broadcast, including any that may want to spy on your output.

In application B i do this:

That is not a good idea. Any app can send a broadcast to this app, either to spoof messages or attempt to cause the app to crash. And on Android 8.0+, you will not receive the broadcast in any case.

I've copied the class implementation in both application so they match.

Perhaps there is a problem in how you did this, as the error message would seem to disagree with your assessment.

I would start by getting rid of the Serializable. Only put things into extras that are guaranteed to be recognized correctly by all parties accessing those extras. So, use simple primitives, or Bundle, or other framework classes, not custom Serializable or Parcelable implementations. Then, see if your more ordinary extras are making it from app to app.

Then, do a better job of inter-process communication:

  • Use an explicit Intent (one with a ComponentName), not an implicit Intent (one with an action string), so that the "broadcast" only goes between the two parties and works around the Android 8.0+ implicit broadcast restrictions

  • Either implement permissions (e.g., android:permission on the <receiver>) or perform signature checks to ensure that the two parties are who you think they are

Solution 2:

MyObject should implement Parcelable, not Serializable...

Post a Comment for "Sending Serializable Object Between Applications With Broadcast Receiver"