每一个应用程序都有自己的进程,进程之间不能互相访问,这叫做应用程序沙漏(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

更多相关文章

  1. Android应用程序执行流程
  2. 自定义 Android Preference——SpinnerPreference的私人定制
  3. 【Android优化】去掉屏幕上方应用程序的名称或标题栏
  4. Android基础之应用程序组件
  5. 如何使Android应用程序获取系统权限【转】

随机推荐

  1. 申请Android(安卓)Maps API Key-及出现的
  2. Android(安卓)数据存储与读取:SQLite
  3. android ICS4.0.3 改变默认字体大小
  4. Android(安卓)实现直接拒接来电
  5. Android:控件布局(相对布局)RelativeLayout
  6. android的apk包签名
  7. Android四种Activity的加载模式
  8. android ConfigChanges
  9. Android中对Handler用法的总结
  10. Androidx学习笔记(7)--常见布局--相对布局