AIDL简单实例

 

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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值