Passing class instances between applications in An

2019-07-24 23:38发布

问题:

I have an application App1 which defines class A and uses instances of this class. What I want to achieve is - after App1 is installed on the device to be able to load App2 which defines and implements class B which is subclass of class A (imported from App1 package); and be able to get an instance of class B in the App1. Class B doesn't add any new interfaces, so using it as class A is ok. But I am not sure how to pass the instance of class B to App1 (preferrably without any user interaction).

Can anyone advise if this is feasible?

回答1:

Taken literally, it is impossible. App1 and App2 will be running on different virtual machines, probably in different processes. There is no way to transport an object between them, any more than you can transport a Java object between an applet and a JavaEE server.

Your options:

  1. Use remote services and AIDL to implement a remote procedure call, effectively giving you "pass by reference" between apps
  2. Use Parceable and Intent extras, effectively giving you "pass by value" between apps