Android AIDL Example

Steps for aidl implemenation:
1.
Create aidl file in src folder as below.
SaInterface.aidl
**********************************
package com.sample.aidl.ui;

interface SAInterface {
String getDownloadFilename (in String name);
}
**********************************
2.
Implement getDownloadFilename(String) method in Service extended class as below.

**********************************
package com.sample.aidl.ui;

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

public class AidlService extends Service {

@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}

private final SAInterface.Stub mBinder = new SAInterface.Stub() {
public String getDownloadFilename(String name) throws RemoteException {
return “You are downloading” + name + “file.”;
}
};
}
*********************************
3.
Make a service call in AidlActivity.

*********************************
package com.sample.aidl.ui;

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.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class AidlActivity extends Activity {

private SAInterface saInterface;
private Context mContext = this;

private String TAG = AidlActivity.class.getSimpleName();
private static final int PROGRESS = 0xDEADBEEF;

private Handler mHandler = new Handler(){

public void handleMessage(android.os.Message msg) {
if (msg.what == PROGRESS) {
try {
String DownloadfileName = saInterface.getDownloadFilename(“AIDL Checking!”);
Log.d(TAG, “DownloadfileName: “+DownloadfileName);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
};

private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
saInterface = SAInterface.Stub.asInterface(service);
}

public void onServiceDisconnected(ComponentName className) {
saInterface = null;
}
};

/** Called with the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
//bind the ServiceConnection object to activity.
mContext.bindService(new Intent(AidlActivity .this,
AidlService.class), mConnection, Context.BIND_AUTO_CREATE);
mHandler.sendMessageDelayed(mHandler.obtainMessage(PROGRESS), 500);
}

@Override
protected void onDestroy() {
// unbind the ServiceConnection object from activity to avoid ServiceConnectionLeaked error.
mContext.unbindService(mConnection);
super.onDestroy();
}
}
*******************************

now check the logcat.

Advertisements

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s