每一个应用程序都有自己的进程,进程之间不能互相访问,这叫做应用程序沙漏(application sandboxing)
为了能用使应用程序能互相访问,Android提供IPC(interprocess communication protocol)
IPC protocol因为需要整理封装(marshaling/un marshaling数据)而变得复杂。
因此.Android提供 AIDL(Android Interface Definition Language),它对使用了类似于java的语法,对IPC轻量级实现,并有AIDL工具自动实现stub的创建
实现步骤:
1.定义 the AIDL interface
2.为remote service实现Stub
3.将remote service提供给本地 local client
第一步:定义 the AIDL interface
package com.marakana;// 定义接口 ]AIDLinterface IAdditionService { // 可传递标记in, out, or inout. // Primitive 类型 (例如 int, boolean, etc.) 仅能而其默认是in. int add(in int value1, in int value2);}
第二步:为remote service实现Stub
/** * 这个类实现了remote service 到 Local client 本地 */ public class AdditionService extends Service { private static final String TAG = "AdditionService"; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate()"); } //返回remote 对象 @Override public IBinder onBind(Intent intent) { return new IAdditionService.Stub() { /** * Implementation of the add() method */ public int add(int value1, int value2) throws RemoteException { Log.d(TAG, String.format("AdditionService.add(%d, %d)",value1, value2)); return value1 + value2; } }; } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy()"); } }
第三步:将remote service提供给本地 local client
利用servcie实现onBind(),就可以连接到remote service
public class AIDLDemo extends Activity { private static final String TAG = "AIDLDemo"; IAdditionService service; AdditionServiceConnection connection; /** * 这个类实现了真实的remote service的连接.它将IBinder类型的对象,也就是Stub的实现类 * 转为 AIDL 接口. asInterface方法是aild工具产生的比较重要的方法 */ class AdditionServiceConnection implements ServiceConnection { public void onServiceConnected(ComponentName name, IBinder boundService) { service = IAdditionService.Stub.asInterface((IBinder) boundService); Log.d(AIDLDemo.TAG, "onServiceConnected() connected"); Toast.makeText(AIDLDemo.this, "Service connected", Toast.LENGTH_LONG) .show(); } public void onServiceDisconnected(ComponentName name) { service = null; Log.d(AIDLDemo.TAG, "onServiceDisconnected() disconnected"); Toast.makeText(AIDLDemo.this, "Service connected", Toast.LENGTH_LONG) .show(); } } /** 将Activity绑定到Service. */ private void initService() { connection = new AdditionServiceConnection(); Intent i = new Intent(); i.setClassName("com.marakana", com.marakana.AdditionService.class.getName()); boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE); Log.d(TAG, "initService() bound with " + ret); } /** 解除绑定. */ private void releaseService() { unbindService(connection); connection = null; Log.d(TAG, "releaseService() unbound."); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initService(); // Setup the UI 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); 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) { Log.d(AIDLDemo.TAG, "onClick failed with: " + e); e.printStackTrace(); } result.setText(new Integer(res).toString()); } }); } /** Called when the activity is about to be destroyed. */ @Override protected void onDestroy() { releaseService(); } }
资源来源:http://marakana.com/forums/android/examples/48.html
源代码: http://marakana.com/static/tutorials/AIDLDemo.zip