android aidl浅析

1.创建aidl文件
在android studio下 点击项目 new–>AIDL–>aidl file
然后点击 make project(ctrl + F9)后 会在 app\build\generated\source\aidl\debug\com\bskuai\aidltest\aidl*IMyAidlInterface.java*

IMyAidlInterface.java 文件就是ide帮我们自动生成的java文件

2.创建service

package com.bskuai.aidltest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.bskuai.aidltest.aidl.IMyAidlInterface;

public class MyService extends Service {

    private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public void add() throws RemoteException {
            Log.e("tag","add");
        }

        @Override
        public void delete() throws RemoteException {
            Log.e("tag","delete");
        }
    };

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return mBinder;
    }
}

3.绑定Service

  private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myAidlInterface = IMyAidlInterface.Stub.asInterface(service);

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            myAidlInterface = null;
        }
    };

   Intent intent = new Intent(this,MyService.class);
        this.bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);

4.配置服务

  <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true"
            android:process=":remote"
            >
        </service>

android:process=”:remote” 让服务运行在 包名:remote 的进程中

以上已经实现不同进程间的通信

5.了解自动生成的IMyAidlInterface.java

在客户端中我们 使用了 myAidlInterface = IMyAidlInterface.Stub.asInterface(service);

asInterface()方法

 public static com.bskuai.aidltest.aidl.IMyAidlInterface asInterface(android.os.IBinder obj) {
            if ((obj == null)) {
                return null;
            }
            android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
            if (((iin != null) && (iin instanceof com.bskuai.aidltest.aidl.IMyAidlInterface))) {
                return ((com.bskuai.aidltest.aidl.IMyAidlInterface) iin);
            }
            return new com.bskuai.aidltest.aidl.IMyAidlInterface.Stub.Proxy(obj);
        }

如果传进来的obj 为null 直接返回 null 通过 queryLocalInterface 得到IInterface对象 如果 该对象不为null 并且和IMyAidlInterface 是同一类型 强转后返回该对象 否则返回Proxy 代理对象

onTransact()负责对自己定义的接口方法的调用

  @Override
        public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
            switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(DESCRIPTOR);
                    return true;
                }
                case TRANSACTION_basicTypes: {
                    data.enforceInterface(DESCRIPTOR);
                    int _arg0;
                    _arg0 = data.readInt();
                    long _arg1;
                    _arg1 = data.readLong();
                    boolean _arg2;
                    _arg2 = (0 != data.readInt());
                    float _arg3;
                    _arg3 = data.readFloat();
                    double _arg4;
                    _arg4 = data.readDouble();
                    java.lang.String _arg5;
                    _arg5 = data.readString();
                    this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5);
                    reply.writeNoException();
                    return true;
                }
                case TRANSACTION_add: {
                    data.enforceInterface(DESCRIPTOR);
                    this.add();
                    reply.writeNoException();
                    return true;
                }
                case TRANSACTION_delete: {
                    data.enforceInterface(DESCRIPTOR);
                    this.delete();
                    reply.writeNoException();
                    return true;
                }
            }
            return super.onTransact(code, data, reply, flags);
        }

调用时通过code来匹配 下面的 int值

 static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
        static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
        static final int TRANSACTION_delete = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值