Android之AIDL

AIDL : Android Interface Definition Language是英文字母的缩写,即Android接口定义语言。主要是用来进行多进程间的通讯。

Android中进行通讯的方式有很多:
(1).如广播(应用A在AndroidManifest.xml中注册指定Action的广播)应用B发送指定Action的广播,A就能收到信息,这样也能看成不同应用之间完成了通讯(但是这种通讯是单向的)。
(2).Android应用程序组件中的3个(Activity、BroadcastReceiver和ContentProvider)都可以进行跨进程访问,另外一个Android应用程序组件Service同样可以。
(3).Android中的 Message也是完成不同程序之间的通讯。

那么为什么Android会推出AIDL呢
android的开发文档给出了这句话:
`Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.
只有当你允许来自不同的客户端访问你的服务并且需要处理多线程问题时你才必须使用AIDL。AIDL是处理多线程的,多客户端并发访问的。
Message是单线程处理。、

Android api中的代码案例:
步骤1:你首先创建一个aidl文件。

package com.example.android;

interface IRemoteService {

    int getPid();

    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

一旦文件被保存,Android的AIDL工具会在gen这个文件夹里自动生成对应的IAdditionService.java这个文件。因为是自动生成的,所以无需改动。这个文件里就包含了Stub,我们接下来要为我们的远程服务实现这个Stub

步骤2:实现接口

private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
    public int getPid(){
        return Process.myPid();
    }
    public void basicTypes(int anInt, long aLong, boolean aBoolean,
        float aFloat, double aDouble, String aString) {
    }
};

步骤3:向客户端公开你的接口

public class RemoteService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
       //返回接口interface
        return mBinder;
    }

    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
        public int getPid(){
            return Process.myPid();
        }
        public void basicTypes(int anInt, long aLong, boolean aBoolean,
            float aFloat, double aDouble, String aString) {

        }
    };
}

现在,当一个客户端(比如一个活动)调用bindService()连接到这个服务,客户端’sonServiceConnected()回调接收mBinder实例的onBind()方法返回的服务。
客户端还必须访问接口类,如果客户端和服务是在单独的应用程序,然后客户端应用程序一定会有一个.aidl文件在src /目录(生成theandroid.os.Binder interface提供客户端访问AIDL方法)。
当客户端接收到 IBinder onServiceConnected()回调,callYourServiceInterface.Stub.asInterface(service)将返回YourServiceInterface类型的参数。例如:

IRemoteService mIRemoteService;
private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {

        mIRemoteService = IRemoteService.Stub.asInterface(service);
    }


    public void onServiceDisconnected(ComponentName className) {
        Log.e(TAG, "Service has unexpectedly disconnected");
        mIRemoteService = null;
    }
};

android官方api中AIDL的解释:

http://www.android-doc.com/guide/components/aidl.html#PassingObjects

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值