Basic AIDL server / client

fabiodelorenzo
Posts: 65
Joined: Thu Oct 03, 2013 5:54 pm

Basic AIDL server / client

Postby fabiodelorenzo » Wed Mar 05, 2014 12:13 am

First the manifest file:

Add the following line in the application section.
Change AidlService with the name of your service:

Code: Select all

<service android:name="AidlService" android:exported="true" />


The Aidl file:

Code: Select all

package info.delorenzo.aidl;
interface IService {
   void setParams(int params);
   void getParams();
}


The Service:

Code: Select all

package info.delorenzo.aidl;

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

public class AidlService extends Service {

   private final String TAG = "AidlService";
   
   private final IService.Stub myBinder = new IService.Stub() {
      public void setParams(int params) throws RemoteException {
         Log.d(TAG,"setParams params="+Integer.toString(params) );
      }
      
      public void getParams() throws RemoteException {
         Log.d(TAG,"getParams" );
      }
   };

   @Override
   public IBinder onBind(Intent arg0) {
      Log.d(TAG,"onBind ! return binder" );
      return myBinder;
   }
   
   @Override
   public void onCreate() {      
      super.onCreate();
      Log.d(TAG,"Service.onCreate() called" );
   }
}


The Client Activity:

Code: Select all

package info.delorenzo.aidl;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class AidlClientActivity extends Activity {

   private static String TAG = "fdl AidlClientActivity";
   IService playerService; // instanzio l'intefaccia

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      Log.d(TAG, "onCreate");
      
      // bind to the service with an intent
      Intent serviceIntent = new Intent();
      serviceIntent.setClassName("info.delorenzo.aidl",
            "info.delorenzo.aidl.AidlService");
      bindService(serviceIntent, mServiceConnection,
            Context.BIND_AUTO_CREATE);
   }
   
   private ServiceConnection mServiceConnection=new ServiceConnection() {
      @Override
      public void onServiceDisconnected(ComponentName name) {
         Log.d(TAG, "onServiceDisconnected");
      }   
      @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
         Log.d(TAG, "onServiceConnected");
         playerService = IService.Stub.asInterface(service);
      }
   };
}

Return to “Services and Bonds”

Who is online

Users browsing this forum: No registered users and 1 guest