AIDL 雙向調(diào)用的注意事項(xiàng)

之前沒有實(shí)戰(zhàn)用過AIDL,最近用的時候稍微麻煩了一些。

預(yù)警:本文不適合做AIDL教程...

總結(jié)如下:

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的回調(diào),后面再說
                scanService.registerCallback(callbackInterface);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

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

注意事項(xiàng):

  • onServiceConnected 和 onServiceDisconnected 都是在主線程回調(diào)
  • onServiceDisconnected只有在服務(wù)進(jìn)程崩潰等異常退出時才會回調(diào),正常unbind不會觸發(fā)
  • 通過獲得的Service(scanService)調(diào)用 aidl接口方法時,是主線程阻塞的調(diào)用,所以理論上接口方法在Service的實(shí)現(xiàn)里面要做異步處理
解綁Service:
if (isBound) {
    activity.unbindService(mConnection);
}

注意事項(xiàng):

  • 綁定成功的才能解綁... 否則會拋出異常...

AIDL雙向調(diào)用

這是AIDL座位IPC機(jī)制的優(yōu)勢之一。

  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里面保存注冊過來的回調(diào),并支持觸發(fā)回調(diào)
    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();
    }

注意事項(xiàng):

  • RemoteCallbackList是Android提供的API,專門用來緩存所有注冊過來的callback。并且Callback實(shí)現(xiàn)了IBinder.DeathRecipient ,Client進(jìn)程意外退出時service能自動將callback刪除,不會有泄漏的哦!?。?!

Parcelable 傳遞數(shù)據(jù)

數(shù)據(jù)Model實(shí)現(xiàn)Parcelable 接口,略...
定義一個 同包名,類名的AIDL文件。

//Model.class
package bulabula.bula;
public class Model implements Parcelable {
public String url;
//Parcelable接口的實(shí)現(xiàn),略...
}

//Model.aidl
package bulabula.bula;

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

注意事項(xiàng):

  • 這倆文件不必堆一起,但是包名必須一致;并且包名也得跟文件目錄保持一致哦(雖然不一致AIDL文件不報錯,但是build不會通過的)
  • 注意在AIDL接口定義時,自定義類型根據(jù)傳入傳出的角色需要設(shè)置in ,out 等屬性哦,不然build會報錯的哦
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容