Android入门:通过AIDL进行进程间通信

来源:互联网 发布:淘宝技巧 知乎 编辑:程序博客网 时间:2024/06/10 00:09


一、AIDL介绍


AIDL:Android Interface Definition Language,接口定义语言;顾名思义,就是定义接口的语言,即利用AIDL可以定义接口;

AIDL简单地说就是进程间通信的方法,类似于Java中的RMI;

AIDL利用Xxx.aidl文件定义接口,通常将此文件放在com.xiazdong.aidl中;

AIDL文件的编写类似于接口的编写,但有一些不同:

(1)AIDL中的接口和方法前不能加上任何访问权限修饰符(如public、private、static、final);
(2)AIDL支持Java基本类型
(3)AIDL中的方法参数如果是非Java基本类型,则要加上in、out、inout;
(4)AIDL对于自定义类型,则需要实现Parselable接口;

AIDL编写后会在gen目录中自动生成java文件;

在自动生成的文件中我们需要了解的方法有:
(1)Xxx xx = Xxx.stub.asInterface(IBinder);//将IBinder类型转换成AIDL类型,在ServiceConnection的onServiceConnected中使用;
(2)Xxx.stub.fun();//调用aidl定义的方法


需要注意的是,由于AIDL文件是两应用通信的桥梁,因此此文件在两应用中都要存在;



二、代码案例



场景说明:此场景和“Android入门:绑定本地服务”文章中的应用功能一样,不同的是加法的服务在AIDLService应用,而调用加法的程序在AIDLClient应用中;

效果和以前一样:




程序组织结构的不同:




AIDLClient代码


AddOp.aidl
package com.xiazdong.aidl;interface AddOp {//除了没有访问权限,其他和接口定义差不多int addService(int a,int b);}

MainActivity.java

package com.xiazdong.aidlclient;import com.xiazdong.aidl.AddOp;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;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;public class MainActivity extends Activity {private EditText op1,op2;private Button button;private TextView result;private AddOp binder ;private ServiceConnection conn = new AddOpServiceConnection();private OnClickListener listener = new OnClickListener(){@Overridepublic void onClick(View v) {int number = 0;try {number = binder.addService(Integer.parseInt(op1.getText().toString()), Integer.parseInt(op2.getText().toString()));} catch (Exception e) {e.printStackTrace();}result.setText("result="+number+"");}};@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        op1 = (EditText)this.findViewById(R.id.op1);        op2 = (EditText)this.findViewById(R.id.op2);        result = (TextView)this.findViewById(R.id.result);        button = (Button)this.findViewById(R.id.button);        button.setOnClickListener(listener);        Intent service = new Intent("com.xiazdong.action");//隐式意图        this.bindService(service, conn, BIND_AUTO_CREATE);    }private class AddOpServiceConnection implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { binder = AddOp.Stub.asInterface(service);//将IBinder转换成接口类型}@Overridepublic void onServiceDisconnected(ComponentName name) {binder = null;}}}


AIDLService代码


AddOp.aidl

package com.xiazdong.aidl;interface AddOp {int addService(int a,int b);}


AddOpService.java

package com.xiazdong.aidlservice;import com.xiazdong.aidl.AddOp;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class AddOpService extends Service {private IBinder binder = new AddOpBinder();@Overridepublic IBinder onBind(Intent intent) {return binder;}public int add(int a,int b){//服务提供的方法通过Binder对象提供给外部return a+b;}private class AddOpBinder extends AddOp.Stub{//继承aidl生成的存根@Overridepublic int addService(int a, int b) throws RemoteException {//实现此方法return add(a,b);//调用服务的方法}}}