本文介绍了使用Parcelable在应用程序之间发送数据的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何将应用程序之间的通信,Android的 - 不仅仅是活动实例之间

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

我成立了一个客户发送一个使者OBJ到服务(在意图发送给服务);该服务创建一个消息 OBJ,并将其发送回使用 messenger.send(消息)的客户。直到我尝试使用Message.obj持有对象这工作得很好。

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.

我创建了自己的 Parcelable MyParcelable 的服务,并把它放入消息。所有的作品,直到消息被解组的客户。该和解组失败,因为客户具有对 MyParcelable 类的访问权限。这是显而易见的 - 他们是在不同的包(比如 com.whatever.myclient com.whatever.myserver )。这是完全错误的方式去了解呢?

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?

我也尝试创建一个包裹和发送的(这两个应用程序将因此拥有访问类) - 但包裹不是 Parcelable 。我看了一下正在使用的类加载器,但不明白类单独的应用程序如何单独装载机(进程,如果我知道Android的架构在这方面)。也就是说,怎么可以在一个类加载器'教导'大约存在于其他类加载器的类?这当然看起来应该有一个明显的'这是你怎么做',但我还没有看到它。

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.

推荐答案

您不能在一个没有这类知识的过程解组的类(因为你已经正确地指出)。如果你想 MyParcelable 将被发送到你的服务,那么你需要包括 MyParcelable 服务在构建,包含正确的的ClassLoader 解组时,由于解组是一个系统线程,不知道是什么 MyParcelable ,即使您是包含在编译时完成被使用。

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.

修改的:

(一)包括MyParcelable成2个不同PKGS并将它们可以看出
  作为同一类

请您共同类的库,它包含在这两个应用程序(应用程序和服务)。您可以将其标记为一个Android库(在Eclipse中,这是正确的根据项目的Andr​​oid属性)或导出为一个JAR并将其导入这两个应用程序。它总是一个好主意的独立的库中的类,而不是把他们直接在应用程序。这样,您就可以重新使用它们在其他项目上;)

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)包括正确的类加载器(我在哪里得到正确的类
  从例如装载机)

您可以得到你的的ClassLoader 从当你执行你的code当前线程。显然,这的ClassLoader 知道你的类,因为它的执行你的code;)例如,在活动#的onCreate(),你可以做 Thread.currentThread.getContextClassLoader()。由于您的 MyParcelable 类,也被列入了服务,你可以做同样获得有效的ClassLoader 来解读。

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.

有关IPC另一个解决方案是定义一个 AIDL接口所以你不'吨有落实 Parcelable

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

这篇关于使用Parcelable在应用程序之间发送数据的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!