How to handle remote objects when an android service restarts?

409 views Asked by At

I have created the following entities in Android :-

  1. Android MyService (apk)
  2. MyServiceClient (jar)
  3. MyApplication (uses MyService via MyServiceClient)

For IPC, I have used AIDL. A sample implementation (of service client) is given below.

AIDL interface - ICLAZZ.aidl(Implemented on the service side, and used internally for IPC with service) Service Client Side - CLAZZ.java (API exposed to developers)

ICLAZZ.aidl

interface ICLAZZ {
 void doSomething();
}

CLAZZ.java

public class CLAZZ 
{
  private ICLAZZ mSvcInstance; //remote object


  // static method, instead of constructor for object creation due to some reason


  public static synchronized CLAZZ getInstance(inputParam) 
  {
  // ICLAZZ remoteObject = get the remote object from service

  if(remoteObject!=null) {
    INSTANCE = new INSTANCE(inputParam);
    INSTANCE.mSvcInstance = remoteObject;
   }

   return INSTANCE;

  }

  private CLAZZ() {
  }


  private CLAZZ(inputParam) {
   // initialize based on inputParam
  }

  public void doSomething() {
   if(mSvcInstance!=null)
     mSvcInstance.doSomething();
  }
};

When the API user calls CLAZZ.getInstance(), I create a remote object instance and save it in the local object of CLAZZ and return the CLAZZ object to the user.

The problem that I am facing is, that in case where the service restarts, all the previous remote objects gets invalidated. However, the API user may have saved the CLAZZ object created earlier and might want to call some functionality on it. This will cause the application to fail. Also, I dont want to keep any global list of API objects created by the application. In the given scenario, is there some mechanism through which I can handle this situation gracefully and provide recovery for existing objects.

1

There are 1 answers

0
edharned On BEST ANSWER

Sounds like the same problem as with RMI on standard Java.

When the remote object fails, simply get a new remote object (Bind on Android) from the service.

Ed