AIDL
AIDL is Android interface definition language.
What? Why? How ?
What? It is a bounded services. This AIDL service will be active till atleast one of the client is exist. It works based on marshaling and unmarshaling concept.
Why? Remote applications can access your service + Multi Threading.(Remote application request).
How? Create the .aidl file Implement the interface Expose the interface to clients
AIDL Service
Section titled “AIDL Service”ICalculator.aidl
// Declare any non-default types here with import statements
interface ICalculator { int add(int x,int y); int sub(int x,int y); }AidlService.java
public class AidlService extends Service {
private static final String TAG = "AIDLServiceLogs"; private static final String className = " AidlService";
public AidlService() { Log.i(TAG, className+" Constructor"); }
@Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. Log.i(TAG, className+" onBind"); return iCalculator.asBinder(); }
@Override public void onCreate() { super.onCreate(); Log.i(TAG, className+" onCreate"); }
@Override public void onDestroy() { super.onDestroy(); Log.i(TAG, className+" onDestroy"); }
ICalculator.Stub iCalculator = new ICalculator.Stub() { @Override public int add(int x, int y) throws RemoteException { Log.i(TAG, className+" add Thread Name: "+Thread.currentThread().getName()); int z = x+y; return z; }
@Override public int sub(int x, int y) throws RemoteException { Log.i(TAG, className+" add Thread Name: "+Thread.currentThread().getName()); int z = x-y; return z; } };
}Service Connection
// Return the stub as interfaceServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, className + " onServiceConnected"); iCalculator = ICalculator.Stub.asInterface(service); }
@Override public void onServiceDisconnected(ComponentName name) {
unbindService(serviceConnection); } };