Android学习笔记--AIDL

来源:互联网 发布:智多星软件教学视频 编辑:程序博客网 时间:2024/06/10 01:26

温故而知新。

AIDL (Android Interface Definition Language) 安卓接口定义语言

官方文档的解释说明:

   AIDL (Android Interface Definition Language) is similar to other IDLs you might have worked with. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC). On Android, one process cannot normally access the memory of another process. So to talk, they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for you with AIDL.

个人理解性翻译:AIDL类似于其他的IDLs,(在使用之前)你需要做一些工作。它需要你在客户端和服务端定义一个统一的程序接口从而实现进程间的通信。因为在安卓系统中,一个进程正常是不能够访问另一个进程的内存资源。因此它们需要将它们自己分解成能够让操作系统识别的基本元素,这样就能够跨越进程间的边界。因为用代码去写这个分配执行的过程是烦琐的,所以安卓提供了ADIL让你去处理它。


怎么理解呢? 说白了就是两个不同的进程实现了一个统一的接口,从而实现了不同进程之间的通信。(这个有点像是多态)

那我们来看看到底是怎么实现这个过程的。 我们来举个简单的例子:

创建两个工程,为了贴近生活方便理解,一个为支付宝,一个为美团。

当我们在美团买东西的时候,选择了支付宝付款,那么美团这个应用就需要和支付宝这个应用进行数据交流,可是在安卓中跨进程通信太难了,我们就可以用到AIDL,具体过程如下:

在支付宝应用中创建一个类继承Service 重写onBind方法 。如下:

public class AliPay extends Service {    @Nullable    @Override    public IBinder onBind(Intent intent) {        return null;    }<pre name="code" class="java">    public String PayMoney(float monkey) {        Toast.makeText(getApplicationContext(), "支付成功", Toast.LENGTH_SHORT).show();        return "支付成功" + monkey;    }
}

接着在支付宝应用中创建一个aidl文件:

使用as工具直接创建就行,eclipse的可以先创建.java然后修改后缀成aidl格式

内容如下:

// Declare any non-default types here with import statements<pre name="code" class="java">// IAliPay.aidlpackage com.example.zhifubao;// Declare any non-default types here with import statementsinterface IAliPay {    /**     * Demonstrates some basic types that you can use as parameters     * and return values in AIDL.     */     String pay(float monkey);}

之后系统会在gen目录下会自动生成一个.java(代理)对象:

(如果该文件没有出现,就按如下按钮,同步一下)

这时候就可以在 应用的onBind方法中返回一个IBind对象。如下:

public IBinder onBind(Intent intent) {        return new IAliPay.Stub(){            @Override            public String pay(float monkey) throws RemoteException {                return ALiPay.this.PayMoney(monkey);            }        };    }

接着再将美团应用的aidl文件复制到支付宝应用中(注意复制的时候包名不可以改变,如果用的是as,直接将as文件包拷过去就行了)如下:


于是支付宝应用的gen目录下也会生成一个.Java文件。

记得在支付宝应用的清单中配置服务信息:

        <service android:name=".ALiPay">            <intent-filter>                <action android:name="alipay"/>            </intent-filter>        </service>


然后为了方便测试,在美团的布局中添加一个按钮并且实现点击事件:

    <Button        android:onClick="pay"        android:text="付款"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>

这时候在支付宝应用中开启Service,传入三个参数,如下:

public class MainActivity extends AppCompatActivity {    private MyServiceConnection serviceConnection;    private IAliPay mIAliPay;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Intent intent = new Intent("alipay");        serviceConnection = new MyServiceConnection();        bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);    }    class MyServiceConnection implements ServiceConnection {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            //将IBinder对象转换为ALipay对象            mIAliPay = IAliPay.Stub.asInterface(service);        }        @Override        public void onServiceDisconnected(ComponentName name) {        }    }    public void pay(View view) throws RemoteException {        mIAliPay.pay(100.f);        Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();    }}

第三个参数就是自动启动Service的意思(一般都是写这个不用管),第二个参数是一个回调参数,当绑定Service 成功的时候可以返回一个IBinder值(一个对象),就是我们在美团应用中传出去的对象。这时我们通过Stub(内部类)中的asInterface方法将这个美团应用中的对象装换成支付宝应用中的对象,然后就能够调用该对象中的方法了。(打开支付宝和美团应用,点击美团中的付款按钮,如下:)


总体的过程就是,A应用实现一个接口,传递出一个方法,B应用实现相同接口,接收A应用中传递出的方法,转换之后调用。  

此文更多的是为了巩固自己已学知识,如有不正确的地方,欢迎指出。


0 0
原创粉丝点击