android 进程间通信之aidl\
android 进程间通信之aidl 多个客户端, 多线程, IPC \
第一步: 创建一个ITestAidl.aidl 文件
Eclipse 下回自动在gen文件夹下生成一个相应的ITestAidl.java文件
第二步: 创建一个服务,
返回的一个binder对象 继承ITestAidl.Stub
第三步: 在客户端, 绑定服务, 通过ServiceConnection 获取IBinder对象,
获取服务器的数据
\
\
Service中返回一个IBinder对象实现了 aidl接口中的Stub (继承了IBinder)
客户端绑定服务,通过ServiceConnection 获取Service中的IBinder,
在客户端进行传输数据, 并返回结果,
\
aidl底层实现
\
\
客户端实现:\
package com.android.hellosumaidl;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
public class HelloSumAidlActivity extends Activity {
IAdditionService service;
AdditionServiceConnection connection;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initService();
Button buttonCalc = (Button)findViewById(R.id.buttonCalc);
buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView)findViewById(R.id.result);
EditText value1 = (EditText)findViewById(R.id.value1);
EditText value2 = (EditText)findViewById(R.id.value2);
@Override
public void onClick(View v) {
int v1, v2, res = -1;
v1 = Integer.parseInt(value1.getText().toString());
v2 = Integer.parseInt(value2.getText().toString());
try {
res = service.add(v1, v2);
} catch (RemoteException e) {
e.printStackTrace();
}
result.setText(Integer.valueOf(res).toString());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseService();
}
/*
* 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;
}
}
\
\
\
将gen文件夹下的.aidl生成的IAdditionService.java
文件 拷出来看以一看:
\
\
public interface IAdditionService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends Binder implements IAdditionService
{
private static final String DESCRIPTOR = "com.android.hellosumaidl.IAdditionService";
/** 构造函数. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/** 创建一个IBinder对象 在需要代理的时候 */
public static IAdditionService asInterface(IBinder obj)
{
if ((obj==null)) {
return null;
}
IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof IAdditionService))) {
return ((IAdditionService)iin);
}
return new IAdditionService.Stub.Proxy(obj);
}
@Override public IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code,
Parcel data, Parcel reply, int flags)
throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_add:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
int _arg1;
_arg1 = data.readInt();
int _result = this.add(_arg0, _arg1);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements IAdditionService
{
private IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
// You can pass the value of in, out or inout
// The primitive types (int, boolean, etc) are only passed by in
@Override public int add(int value1, int value2) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(value1);
_data.writeInt(value2);
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
// You can pass the value of in, out or inout
// The primitive types (int, boolean, etc) are only passed by in
public int add(int value1, int value2) throws android.os.RemoteException;
}
\