Pages

Sunday, April 24, 2016

Remote Service Android Developer Tutorial Part 9


Services typically are required to run for a long time and hence should run in their own thread. Such services can be invoked by any number of clients who want to connect to the service, invoke a few methods on the service and finally release the service, probably to serve more clients or close down.

Here, I would like to introduce you to the concept of connecting to a remote service and the kind of support provided by the android platform for the same.

We have earlier seen how local services can be created and used. The difference between the two mainly is that the local service runs in the same process as the application that started it and hence the life of the local service is dependent on the life of the said application while remote service can run in its own process. This causes a challenge of inter-process communication. If one process wants to communicate with another process, the object that is passed between the two needs to be marshaled.

For this purpose, Android provides the AIDL (Android Interface Definition Language) tool that handles the marshaling as well as the communication.

The service has to declare a service interface in an aidl file and the AIDL tool will automatically create a java interface corresponding to the aidl file. The AIDL tool also generates a stub class that provides an abstract implementation of the service interface methods. The actual service class will have to extend this stub class to provide the real implementation of the methods exposed through the interface.

The service clients will have to invoke the onBind() method on the service to be able to connect to the service. The onBind() method returns an object of the stub class to the client. Here are the code related code snippets:

The AIDL file:
package com.collabera.labs.sai;

interface IMyRemoteService {

      int getCounter();
}

Once you write this AIDL file (.aidl) in eclipse, it will automatically generate the Remote interface corresponding to this file. The remote interface will also provide a stub inner class which has to have an implementation provided by the RemoteService class. The stub class implementation within the service class is as given here:

private IMyRemoteService.Stub myRemoteServiceStub = newIMyRemoteService.Stub() {
            public int getCounter() throws RemoteException {
                  return counter;
            }
      };
The onBind() method in the service class:
      public IBinder onBind(Intent arg0) {
            Log.d(getClass().getSimpleName(), "onBind()");
            return myRemoteServiceStub;
      }

Now, let us quickly look at the meat of the service class before we move on to how the client connects to this service class. My RemoteService class is just incrementing a counter in a separate thread. This thread is created in the onStart()method as this gets certainly called whether the service is connected to by a call to startService(intent).Please read the lifecycle of a service if this needs more clarity. Here are the over-ridden onCreate(), onStart()and onDestroy()methods. Note that the resources are all released in the onDestroy()method.

      public void onCreate() {
            super.onCreate();
            Log.d(getClass().getSimpleName(),"onCreate()");
      }
      public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            serviceHandler = new Handler();
            serviceHandler.postDelayed(myTask, 1000L);
            Log.d(getClass().getSimpleName(), "onStart()");
      }
      public void onDestroy() {
            super.onDestroy();
            serviceHandler.removeCallbacks(myTask);
            serviceHandler = null;
            Log.d(getClass().getSimpleName(),"onDestroy()");
      }

A little explanation: In the onStart() method, I created a new Handler object that will spawn out a new task that implements the Runnableinterface. This thread does the job of incrementing the counter. Here is the code for the Task class – an inner class of the RemoteServiceclass.

class Task implements Runnable {
      public void run() {
            ++counter;
            serviceHandler.postDelayed(this,1000L);
            Log.i(getClass().getSimpleName(), "Incrementing counter in the run method");
      }
}

An object of this Taskclass is passed to the serviceHandler object as a message that needs to be executed after 1 second. The Taskclass implements the run() method in which we repeatedly post the same message to the serviceHandler. Thus, this becomes a repeated task till all the messages in the serviceHandlerqueue are deleted by calling the removeCallbacks()method on the serviceHandler in the destroy()method of the RemoteService class.

Note that the onDestroy()method thus stops this thread and set the service

Related Posts by Categories

0 comments:

Post a Comment