package com.android.hellosumaidl; // Interface declaration interface IAdditionService { // You can pass the value of in, out or inout // The primitive types (int, boolean, etc) are only passed by in int add(in int value1, in int value2); }
package com.android.hellosumaidl; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; /* * This class exposes the service to client */ public class AdditionService extends Service { @Override public void onCreate() { super.onCreate(); }
@Override public IBinder onBind(Intent intent) { return new IAdditionService.Stub() { /* * Implement com.android.hellosumaidl.IAdditionService.add(int, int) */ @Override public int add(int value1, int value2) throws RemoteException { return value1 + value2; } }; }
@Override public void onDestroy() { super.onDestroy(); } }
/* * This inner class is used to connect to the service */ class AdditionServiceConnection implements ServiceConnection { public void onServiceConnected(ComponentName name, IBinder boundService) { service = IAdditionService.Stub.asInterface((IBinder)boundService); Toast.makeText(HelloSumAidlActivity.this, "Service connected", Toast.LENGTH_LONG).show(); }
public void onServiceDisconnected(ComponentName name) { service = null; Toast.makeText(HelloSumAidlActivity.this, "Service disconnected", Toast.LENGTH_LONG).show(); } }
/* * This inner class is used to connect to the service */ class AdditionServiceConnection implements ServiceConnection { public void onServiceConnected(ComponentName name, IBinder boundService) { service = IAdditionService.Stub.asInterface((IBinder)boundService); Toast.makeText(HelloSumAidlActivity.this, "Service connected", Toast.LENGTH_LONG).show(); }
public void onServiceDisconnected(ComponentName name) { service = null; Toast.makeText(HelloSumAidlActivity.this, "Service disconnected", Toast.LENGTH_LONG).show(); } }
/* * This function connects the Activity to the service */ private void initService() { connection = new AdditionServiceConnection(); Intent i = new Intent(); i.setClassName("com.android.hellosumaidl", com.android.hellosumaidl.AdditionService.class.getName()); boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE); }
/* * This function disconnects the Activity from the service */ private void releaseService() { unbindService(connection); connection = null; } }