天天看点

AIDL入门1.用途2.名词3.使用AIDL步骤4.一个栗子5.源码

android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信。

为了使其他的应用程序也可以访问本应用程序提供的服务,android系统采用了rpc方式来实现。与很多其他的基于rpc的解决方案一样,android使用一种接口定义语言idl来公开服务的接口。我们知道4个android应用程序组件中的3个(activity、broadcastreceiver和contentprovider)都可以进行跨进程访问,另外一个android应用程序组件service同样可以。因此,可以将这种可以跨进程访问的服务称为aidl服务。

aidl(android interface definition language):android接口定义语言

idl(interface definition language):接口定义语言

rpc(remote procedure call):远程过程调用

ipc(inter-process communication):进程间通信

1.create the .aidl file

this file defines the programming interface with method signatures.

2.implement the interface

the android sdk tools generate an interface in the java programming language, based on your .aidl file. this interface has an inner abstract class named stub that extends binder and implements

methods from your aidl interface. you must extend the stub class and implement the methods.

3.expose the interface to clients

implement a service and override onbind() to return your implementation of the stub class.

来源: <>

有两个应用:aidlserver和aidlclient.

aidlserver提供一个add方法

<code></code>

int add(int a,int b)

{

    return a+b;

}

而aidlclient中会通过aidl调用此方法计算5+9的答案.

核心代码:

1. myaidl.aidl(aidlserver)

package com.example.aidlserver;

interface myaidl

    int add(int a,int b);

2. myserver.java(aidlserver)

import android.app.service;

import android.content.intent;

import android.os.ibinder;

import android.os.remoteexception;

import android.util.log;

public class myserver extends service

    public class myserverimpl extends com.example.aidlserver.myaidl.stub

    {

        @override

        public int add(int a, int b) throws remoteexception

        {

            return a+b;

        }

    }

    @override

    public ibinder onbind(intent intent)

        return new myserverimpl();

    public void oncreate()

        super.oncreate();

3. mainactivity.java(aidlclient)

package com.example.aidlclient;

import com.example.aidlserver.myaidl;

import android.os.bundle;

import android.app.activity;

import android.content.componentname;

import android.content.context;

import android.content.serviceconnection;

import android.view.menu;

import android.view.view;

import android.view.view.onclicklistener;

import android.widget.button;

import android.widget.textview;

public class mainactivity extends activity

    private myaidl mmyaidl;

    private textview tv;

    private button bt;

    private serviceconnection serviceconnection = new serviceconnection()

        @override

        public void onserviceconnected(componentname

name, ibinder service)

        {

            mmyaidl=myaidl.stub.asinterface(service);

        }

        public void onservicedisconnected(componentname

name)

    };

    protected void oncreate(bundle

savedinstancestate)

        super.oncreate(savedinstancestate);

        setcontentview(r.layout.activity_main);

        bt=(button)findviewbyid(r.id.bt);

        tv=(textview)findviewbyid(r.id.tv);

        bt.setonclicklistener(new onclicklistener()

            @override

            public void onclick(view

v)

            {

                try

                {

                    int a=5;

                    int b=9;

                    int r=0;

                    tv.settext(mmyaidl.add(a, b));

                }

                catch(exception e)

                    e.printstacktrace();

            }

        });

        bindservice(new intent("com.example.aidlserver.myaidl"),

serviceconnection , context.bind_auto_create);

aidlserver和aidlclient的源码上传网盘地址: