每一个应用程序都有自己的进程,进程之间不能互相访问,这叫做应用程序沙漏(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中界面实现全屏显示的两种方式
  3. 基于Platinum库的DLNA开发
  4. Android的碎片fragment和activity之前的关系 以及MVP模式详解
  5. Theme(主题) Style(风格)
  6. 自定义 Android(安卓)Preference——SpinnerPreference的私人定
  7. 如何实现手势缩放图片
  8. android 自定义ButtonTab , ActivityGroup 动态加载 activity
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. Android事件分发机制五:面试官你坐啊
  2. 基于组件化/模块化的重构探索实践
  3. 20172323 2017-2018-2《程序设计与数据结
  4. Android(安卓)自定义View之展开收起的Lay
  5. 浅析Android下的Android.mk文件
  6. Android真机连接本地部署的Tomcat问题
  7. Android中ViewPager+Fragment取消(禁止)
  8. Android(安卓)外部存储与内部存储详解
  9. android mvvm viewmodel 拿到view的值_一
  10. Android事件分发机制五:面试官你坐啊