Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using Parcelable the right way to send data between applications?

I am trying to understand how to communicate between applications in Android - not just between Activity instances.

I set up a 'client' that sends a Messenger obj to a Service (in the Intent sent to the service); the service creates a Message obj and sends it back to the 'client' using messenger.send(message). This works fine until I try to use the Message.obj to hold an object.

I created my own Parcelable class MyParcelable in the service and put it into the message. All works until the message is unmarshalled in the 'client'. The unmarshall fails because the 'client' has no access to the MyParcelable class. That's obvious - they are in different packages (say com.whatever.myclient and com.whatever.myserver). Is this completely the wrong way to go about this?

I have also tried creating a Parcel and sending that (both applications would thereby have access to the class) - but Parcel is not Parcelable. I have read about class loaders being used but do not understand how separate class loaders in separate applications (processes, if I understand Android architecture in that regard). That is, how can one class loader be 'taught' about a class that exists in the other class loader? It certainly seems like there should be an obvious 'this is how you do it' but I have not seen it yet.

like image 684
Art Swri Avatar asked Mar 25 '13 01:03

Art Swri


People also ask

What is Parcelable used for?

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.

Why do we use Parcelable in Android?

Parcelable and Bundle objects are intended to be used across process boundaries such as with IPC/Binder transactions, between activities with intents, and to store transient state across configuration changes.

What is difference between serializable and Parcelable?

Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations. Parcelable is an Android specific interface where you implement the serialization yourself.

Can we use Parcelable in Java?

Serializable is also an interface and it's not a part of Android SDK. Parcelable is the Android implementation of Java serializable. Parcelable is relatively fast as compared to the java serialization. It's recommended to use Parcelable.


1 Answers

You cannot unmarshall a class at a process that has no knowledge of this class (as you already correctly pointed out). If you want MyParcelable to be sent to your Service, then you need to include MyParcelable in the Service at build, and include the right ClassLoader to be used when unmarshalling, because unmarshalling is done by a system thread, which doesn't know what MyParcelable is even if you included it at compile time.

EDIT:

(a) include MyParcelable into 2 different pkgs and have them be seen as the same class

Make a library of your common classes and include it in both applications (the application and the Service). You can either mark it as an Android library (in Eclipse this is right under the project's Android properties) or export it as a JAR and import it in both apps. It's always a good idea to have independent classes in a library instead of putting them in the apps directly. This way you can reuse them on other projects ;)

(2) include the right ClassLoader (where do I get the right class loader from, for example)

You can get your ClassLoader from the current thread when you're executing your code. Obviously this ClassLoader knows about your classes because it's executing your code ;) For example in Activity#onCreate(), you can do Thread.currentThread.getContextClassLoader(). Since your MyParcelable class is also included on the Service, you can do the same to get a valid ClassLoader to unmarshall.

Another solution for IPC is to define an AIDL interface so you don't have to implement Parcelable.

like image 124
m0skit0 Avatar answered Sep 24 '22 15:09

m0skit0