导航号,我的单页导航
文章目录

前言

简单记录一下aidl的使用,方便自己查阅和复习。

正文

因为客户端和服务端都需要引入相同的aidl的定义,因此这里单独吧aidl文件作为一个lib库。

Biu2Aidl库

Biu2Aidl是lib库,存放客户端和服务端共同的aidl定义和Constant类。

IBook.aidl

/Biu2Aidl/src/main/aidl

// IBook.aidl
package com.biumall.biu2aidl;

interface IBook {
    //发送Book信息
    void sendBookInfo(String bookName);
}

这个只是开始,所以定义一个简单点的。

AidlConstant.java

定义一些常量,方便统一修改。

public class AidlConstant {

    public final static String TAG= "Aidl_";

    public static final String SERVER_SERVICE_PACKAGE = "com.biumall.biu2server";
    public static final String SERVER_SERVICE_NAME = "com.biumall.biu2server.service.AidlServerService";
    public static final String SERVER_SERVICE_AIDL_ACTION = "com.biumall.action.START_SERVER_SERVICE";
}

Biu2Server服务端

AndroidManifest.xml
<service
    android:name=".service.AidlServerService"
    android:exported="true">
    <intent-filter>
        <action android:name="com.biumall.action.START_SERVER_SERVICE" />
    </intent-filter>
</service>
AidlServerService.java

这个就是返回BookIBinder,并实现aidl中的接口,这样就客户端就可以跟服务端通信了。

public class AidlServerService extends Service {
    private static final String TAG = "AidlServerService_";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        //返回IBinder
        return new BookIBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }

    //
    private static class BookIBinder extends IBook.Stub {
        @Override
        public void sendBookInfo(String bookName) throws RemoteException {
            Log.d(TAG, "sendBookInfo bookName : " + bookName);
        }
    }
}

Biu2Client客户端

ClientApp.getContext()是实现Application类中返回的Context,这里懒得附上。

下面是在定义绑定和解除绑定的。

//获取到mIBook后就可以跟服务端进行通信
private IBook mIBook;

//绑定
private void bindService() {
    Log.d(TAG, "bindService: ");
    if (null == mIBook) {
        Intent intent = new Intent();
        intent.setPackage(AidlConstant.SERVER_SERVICE_PACKAGE);
        intent.setAction(AidlConstant.SERVER_SERVICE_AIDL_ACTION);
        boolean bind = ClientApp.getContext().bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
        Log.d(TAG, "bindService MediaService.class " + (bind ? "success ^_^" : "fail ~_~"));
    }
}

//解除绑定
private void unbindService() {
    Log.d(TAG, "unbindService: ");
    if (null != mIBook) {
        ClientApp.getContext().unbindService(mServiceConnection);
        mIBook = null;
    }
}

private final ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.d(TAG, "onServiceConnected name : " + name + " , service :" + service);
        mIBook = IBook.Stub.asInterface(service);
        try {
            //向服务端发送消息
            mIBook.sendBookInfo("Hello word!");
            //linkToDeath监听
            mIBook.asBinder().linkToDeath(new IBinder.DeathRecipient() {
                @Override
                public void binderDied() {
                    Log.d(TAG, "binderDied : ");
                }
            }, 0);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.d(TAG, "onServiceDisconnected name : " + name);
    }
};

参考文章

© 版权声明
导航号,我的单页导航

暂无评论

暂无评论...