aidl的使用

来源:互联网 发布:商品抢购软件 编辑:程序博客网 时间:2024/06/05 17:58

一、aidl介绍

我们可以去官方文档看看官方对aidl的介绍,https://developer.android.com/guide/components/aidl.html
AIDL(Android 接口定义语言)与您可能使用过的其他 IDL 类似。 您可以利用它定义客户端与服务使用进程间通信 (IPC) 进行相互通信时都认可的编程接口。 在 Android 上,一个进程通常无法访问另一个进程的内存。 尽管如此,进程需要将其对象分解成操作系统能够识别的原语,并将对象编组成跨越边界的对象。 编写执行这一编组操作的代码是一项繁琐的工作,因此 Android 会使用 AIDL 来处理。
注:①IPC:进程间通信(InterProcess Communication)是指在不同进程之间传播或交换信息。
②在Android中,每个进程使用的是独立的虚拟机,这样的好处是安全,进程间没有交流,还有就是如果有个进程崩溃出问题了,其他的进程不受任何影响。所以说Android间各个应用之间不能直接通信的,所以这个时候aidl,Binder,就出现了。

二、aidl的使用

aidl是跨进程通信,所以我们得创建两个应用,提供服务的称之为服务端,调用端称之为客户端。
1、创建aidl文件
android studio已经提供创建aidl文件了
这里写图片描述
aidl文件都是以.aidl为后缀的,创建完aidl文件后,在项目中会自动生成一个与Java目录同级的aidl目录
这里写图片描述

在新建的IMyAidlInterface.aidl中写了一个简单的求和方法

// IMyAidlInterface.aidlpackage com.aidl.demo;// Declare any non-default types here with import statementsinterface IMyAidlInterface {//    int Sum(int num1,int num2);    /** Demonstrates some basic types that you can use as parameters     * and return values in AIDL.     */    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,            double aDouble, String aString);}

编译下项目就会发现
这里写图片描述
build下就会生成对应的Java类。

aidl文件不管是客户端还是服务端都需要,而且要求一模一样,所以在一端写完直接连同目录一块copy到另一端就行了。

2、实现aidl接口
因为需要服务端给客户端提供服务,所以这里在服务端定义一个Service,这个Service实现上面定义的接口。

public class AidlService extends Service {    @Override    public void onCreate() {        super.onCreate();        Log.d("tag","AidlService");    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        return aidlInterface;    }    private IMyAidlInterface.Stub aidlInterface = new IMyAidlInterface.Stub(){        @Override        public int Sum(int num1, int num2) throws RemoteException {            return num1 + num2;        }    };}

这里写图片描述
因为用的是Binder来通信,在客服端bindService就会调用AidlService的onBind()方法,这个方法返回的就是个Binder,有兴趣的会可以在编译生成的IMyAidlInterface的Java类中查看,

/** Local-side IPC implementation stub class. */public static abstract class Stub extends android.os.Binder implements com.aidl.demo.IMyAidlInterface

所以IMyAidlInterface.Stub就是一个Binder,当然也要实现这个接口的方法,就是那会写的求和的方法。

接下来在清单文件中配置这个Service,四大组件都得配,要不然不起作用

<service android:name=".AidlService"            android:exported="true"/>

android:exported=”true”是必须的配的。它的主要作用是是否支持其它应用调用当前组件。默认值如果包含有intent-filter 默认值为true; 没有intent-filter默认值为false。所以不配android:exported=”true”,写个intent-filter也是可以的。否则客服端调用的是会报:

java.lang.SecurityException: Not allowed to bind to service Intent { cmp=com.pursue.it.aidlserver/.AidlService }

服务端就写完了

3、通信
在客户端中我写了个简单的UI就是两个数相加求和,调用服务端的方法来计算。
布局文件activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">   <EditText       android:id="@+id/et_number1"       android:layout_width="match_parent"       android:layout_height="wrap_content"/>    <TextView        android:text="+"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <EditText        android:id="@+id/et_number2"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <TextView        android:id="@+id/tv_sum"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <Button        android:id="@+id/btn_sum"        android:text="计算结果"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

MainActivity的代码:

package com.example.xiangyong.aidldemo;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import com.aidl.demo.IMyAidlInterface;public class MainActivity extends AppCompatActivity {    private EditText et_number1;    private EditText et_number2;    private TextView tv_sum;    private Button btn_sum;    private IMyAidlInterface myAidl;    private ServiceConnection conn = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            //连接到Service            Log.d("Tag","onServiceConnected======");            myAidl = IMyAidlInterface.Stub.asInterface(service);            //通过Binder获取IMyAidlInterface的实例        }        @Override        public void onServiceDisconnected(ComponentName name) {            //未连接或者断开连接            //实际开发中这个地方做回收进程通信的一些资源            Log.d("Tag","onServiceDisconnected======");        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        bindAidlService();    }//绑定服务    private void bindAidlService(){        Log.d("Tag","bindAidlService======");        Intent intent = new Intent();        intent.setComponent(new ComponentName("com.pursue.it.aidlserver","com.pursue.it.aidlserver.AidlService"));        bindService(intent, conn, Context.BIND_AUTO_CREATE);    }    private void initView() {        et_number1 = (EditText) findViewById(R.id.et_number1);        et_number2 = (EditText) findViewById(R.id.et_number2);        tv_sum = (TextView) findViewById(R.id.tv_sum);        btn_sum = (Button) findViewById(R.id.btn_sum);        btn_sum.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int num1 = Integer.parseInt(et_number1.getText().toString());                int num2= Integer.parseInt(et_number1.getText().toString());                try {                   int sum =  myAidl.Sum(num1,num2);//调用IMyAidlInterface的求和方法                    tv_sum.setText(sum+"");                } catch (RemoteException e) {                    e.printStackTrace();                }            }        });    }}

代码很简单就是一个绑定服务,在绑定服务的回调中根据返回的Binder获取IMyAidlInterface的实现类(就是在服务端Service中的那个实现类),在求和的地方调用IMyAidlInterface的sum()方法。

客户端也写好后,将两个应用运行到同一个手机上就行了。这是个简单调用方法的进程通信。

原创粉丝点击