AIDL 双向调用的注意事项

字数 463阅读 97

之前没有实战用过AIDL,最近用的时候稍微麻烦了一些。

预警:本文不适合做AIDL教程...

总结如下:

AIDL 接口声明

建个文件,build一下 略...

Bound Service绑定与解绑

绑定到service:
boolean isBound = false;
Intent serviceIntent = new Intent(activity, QRCodeScanService.class);
serviceIntent.setAction(IQRCodeScanInterface.class.getName());
activity.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
isBound = true;

-------------------------分割-------------
//mConnection  核心代码
private class QRCodeScanServiceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            scanService = IQRCodeScanInterface.Stub.asInterface(service);
            try {
                Logger.d(TAG, "QRCodeScanService Connected");
                //注册Service对Client的回调,后面再说
                scanService.registerCallback(callbackInterface);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (scanService != null) {
                Logger.d(TAG, "QRCodeScanService Disconnected");
                scanService = null;
            }
        }
    }

注意事项:

  • onServiceConnected 和 onServiceDisconnected 都是在主线程回调
  • onServiceDisconnected只有在服务进程崩溃等异常退出时才会回调,正常unbind不会触发
  • 通过获得的Service(scanService)调用 aidl接口方法时,是主线程阻塞的调用,所以理论上接口方法在Service的实现里面要做异步处理
解绑Service:
if (isBound) {
    activity.unbindService(mConnection);
}

注意事项:

  • 绑定成功的才能解绑... 否则会抛出异常...

AIDL双向调用

这是AIDL座位IPC机制的优势之一。

  1. 再建立一个Client 端Callback 定义的AIDL文件
package bulabula.bula;

// Declare any non-default types here with import statements
interface IQRCodeScanCallbackInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
    void notifyResult(String result);
}

  1. 在之前Service的AIDL文件中添加注册callback的接口
package  bulabula.bula;

import  bulabula.bula.IQRCodeScanCallbackInterface;

// Declare any non-default types here with import statements
interface IQRCodeScanInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);

    void scanResult(in Image imageInfo);

    void registerCallback(IQRCodeScanCallbackInterface callback);
}
  1. 客户端在connect到service后注册callback
//就是上面mConnection里的代码了
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            scanService = IQRCodeScanInterface.Stub.asInterface(service);
            try {
                Logger.d(TAG, "QRCodeScanService Connected");
                scanService.registerCallback(callbackInterface);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
  1. service里面保存注册过来的回调,并支持触发回调
    private RemoteCallbackList<IQRCodeScanCallbackInterface> mCallBacks = new RemoteCallbackList<>();
private final IQRCodeScanInterface.Stub mBinder = new IQRCodeScanInterface.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
            //do nothing
        }

        @Override
        public void scanResult(final Image imageInfo) throws RemoteException {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(10000); // 模拟耗时操作...
                        notifyClient("pang");
                    } catch (Exception e) {
                        Logger.d(TAG, e.getMessage(), e);
                    }
                }
            };
            thread.start();
        }

        @Override
        public void registerCallback(IQRCodeScanCallbackInterface callback) throws RemoteException {
            mCallBacks.register(callback);
        }
    };

private void notifyClient(String result) throws RemoteException {
        int count = mCallBacks.beginBroadcast();
        for (int i = 0; i < count; i++) {
            IQRCodeScanCallbackInterface broadcastItem = mCallBacks.getBroadcastItem(i);
            if (broadcastItem != null) {
                broadcastItem.notifyResult(result);
            }
        }
        mCallBacks.finishBroadcast();
    }

注意事项:

  • RemoteCallbackList是Android提供的API,专门用来缓存所有注册过来的callback。并且Callback实现了IBinder.DeathRecipient ,Client进程意外退出时service能自动将callback删除,不会有泄漏的哦!!!!

Parcelable 传递数据

数据Model实现Parcelable 接口,略...
定义一个 同包名,类名的AIDL文件。

//Model.class
package bulabula.bula;
public class Model implements Parcelable {
public String url;
//Parcelable接口的实现,略...
}

//Model.aidl
package bulabula.bula;

// Declare any non-default types here with import statements
parcelable Model; //只需要这一句就行了

注意事项:

  • 这俩文件不必堆一起,但是包名必须一致;并且包名也得跟文件目录保持一致哦(虽然不一致AIDL文件不报错,但是build不会通过的)
  • 注意在AIDL接口定义时,自定义类型根据传入传出的角色需要设置in ,out 等属性哦,不然build会报错的哦