AIDL通讯就像客户端和服务器之间的通讯
1、客户端
1.1、首先对于客户端,创建包名.类名
android.content.clientInterface.aidl
package android.content;
interface clientInterface{
int test_cal(int num1,int num2);
}
1.2、客户端连接相应的服务
ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
//Log.i("123", "onServiceDisconnected");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
//Log.i("123", "onServiceConnected");
mService = clientInterface.Stub.asInterface(service);
}
};
Intent mIntent = new Intent("android.content.clientInterface");
this.mContext.bindService(mIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
2、服务端
2.1、对于服务端一样,创建包名.类名
android.content.clientInterface.aidl
package android.content;
interface clientInterface{
int test_cal(int num1,int num2);
}
2.2、创建service来监听客户端的调用函数
public class MoniService extends Service {
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
}
private final clientInterface.Stub mBinder = new clientInterface.Stub(){
@Override
public int test_cal(int a, int b) throws RemoteException{
Log.i(TAG,"add a = " + a + "b = " + b);
return a+b;
}
};
}