Monday, November 1, 2010

Android Remote Service


This tutorial is to demonstrate usage of AIDL Interface and interacting 2 Android applications through Service. You can get the complete example source code- RemoteService Example and RemoteService Calling Example 

As we all know Android Service concept is good for tasks running in the background. Based on scope of accessibility services are of 2 types- Local and Remote. As the name indicates, Local Service can be accessed only within the process or application which it is part of. On the other hand, Remote Services can be accessed from different process/application and thus it posses inter-application communication use cases e.g. you are running one service to provide Geo Location for Cell ID. So, any application can read the Cell-ID and access your service to get Geo Co-ordinates. 

As the usability increases so as the building blocks for creating Remote Service. The first challenge is to do inter-process communication so that one application running on one process can access services running on another process. To achieve this IPC (Inter Process Communication) Android has provided a concept of AIDL Interface which acts as the communication interface between 2 processes.  

Following section explains steps to create a Remote Service and an Application to call that service- 

Steps to create a Remote Service
1.     Create an AIDL Interface. Define methods which you are planning to expose to client applications. AIDL interface syntax is similar to Java. It supports most of the Java primitive data types and does not need explicit import statements. Though for custom classes you need to add specific import statements.
a)     Create the interface file, PrasRemote.aidl under pras.service.aidl package. I have defined one method sayHello(String name) which I’ll access from client application and pass some name and it will return “Hello [Name]
         package pras.service.aidl;
         interface PrasRemote {
String sayHello(String name);
}  
b)    If there is no syntax error, it will generate a Java file under gen folder pras.service.aidl.PrasRemote.java

2.     Define the implementation of the methods defined in Step 1.
package pras.service;

import android.os.RemoteException;
import pras.service.aidl.PrasRemote;

/**
 * Implementation of AIDL interface method
 *
 * @author prasanta
 */
public class MethodImpl extends PrasRemote.Stub {

      public String sayHello(String name) throws RemoteException {
            return "Hello "+ name;
      }
}

3.     Define the Service.
package pras.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

/**
 * The actual Service class which will receive remote calls from Clients
 *
 * @author prasanta
 */
public class PrasService extends Service {

      String TAG = "PrasService";
      @Override
      public IBinder onBind(Intent intent) {
            Log.i(TAG, "onBind_________________________");
            //Return Instance of MethodImpl.java which implements AIDL Interface
            return new MethodImpl();
      }
}

4.     Add entries into Manifest for the service defined in Step 3
[service android:name=".PrasService"]
[intent-filter]
            [action android:name="com.my.service.REMOTE_SERVICE"/]
      [/intent-filter]
[/service]
An intent action having value com.my.service.REMOTE_SERVICE need to be used to call this Service from Client Application

Steps to Call the Remote Service-
1.     Create a new Android project. 


2.     Copy the AIDL file i.e. PrasRemote.aidl file into this new Application Project. Please make sure you are keeping the .aidl file in the same package name as you define in RemoteService application i.e. pras.service.aidl Otherwise it might cause java.lang.SecurityException : Binder invocation to an incorrect interface  
3.     Create a Listener class which implements ServiceConnection Interface and handle call backs when connected to Remote Service. ServiceConnection interface has 2 methods- onServiceConnected(ComponentName cmp, IBinder binder) which will get invoked when client is connected to RemoteService and onServiceDisconnected(ComponentName cmp) if unexpectedly it gets disconnected.
onServiceConnected will provide a IBinder instance which will be casted to get instance of the Interface i.e. PrasRemote. Now you can call the method i.e. sayHello() using this instance.


PrasRemote remoteIntf = PrasRemote.Stub.asInterface(binder);
try{
   String remoteMsg = remoteIntf.sayHello("Prasanta");
}catch(RemoteException ex){}  


4.     Define an UI to Bind and Unbind RemoteService. Execute following when user click on “Bind_Remote_Service” button-
RemoteServiceCon serCon = new RemoteServiceCon(this);
Intent intent = new Intent("com.my.service.REMOTE_SERVICE");
bindService(intent, serCon, Context.BIND_AUTO_CREATE);

To unbind-
unbindService(serCon);

Kindly note: bindService() and unbindService() are methods of Context.java

  

10 comments:

  1. Prasanta, Nice explanation about remote service. Could you explain more about, if i have to share a class object through remote service, i read the class have to extend parcelable class and do some necessary steps. Could you explain about that if you have come across ? thanks, mani

    ReplyDelete
  2. Thanks...!! a lot... i too share some learnings on android.. i am working on a android tablet..good to be friend with you..!!

    ReplyDelete
  3. Is it possible to use AIDL for C++ based native level?

    ReplyDelete
  4. Regarding AIDL for C++ based native:
    Well I never tried that...
    But the probable solution is, using JNI you can attach your C/C++ code. This will be part of your Remote Service Implementation logic. e.g. in my example, sayHello() can use a Native C/C++ method call.
    You can use NDK to create C/C++ and Java integrated APK.
    hope it helps!

    ReplyDelete
  5. Prasanta, thanks; I'd been battling this a long time, I was sure I'd tried to get that aidl into my client package as you described before but I must have had something else wrong at the same time.
    Anyway, it looks like my test is working now. And, I see, you know about C++ on Android - my primary lang - so I might be back to question you on that.
    Many thanks
    Steve

    ReplyDelete
  6. Thanks Buddy... It was great help. I was trying to work it out properly for last 3 days

    ReplyDelete
  7. Nice page, thanks. What is the difference if doing the same with Messenger instead of aidl? I have a messenger service that I try to bind using your example but get "Unable to start service Intent { act=com.my.service.REMOTE_SERVICE }: not found".

    The client is in different apk and package, I build them by including services build time classes folder in the class path when building client.

    ReplyDelete
    Replies
    1. Please ignore my question. It works the same way and well with AIDL and Messenger, I just had a bug in my code (missed call to super.onCreate() in the services onCreate).

      Thanks for you helpful blog post.

      Delete
  8. Hi Prashanth,
    Nice page, explanations are simple to follow. I was not able to download the code, can you pls shared the code.
    -Thanks & regards,
    Manjunath S

    ReplyDelete