日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 中的aidl

發布時間:2025/5/22 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 中的aidl 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.什么是aidl:aidl是 Android Interface definition language的縮寫,一看就明白,它是一種android內部進程通信接口的描述語言,通過它我們可以定義進程間的通信接口
icp:interprocess communication :內部進程通信

?

2.既然aidl可以定義并實現進程通信,那么我們怎么使用它呢?文檔/android-sdk/docs/guide/developing/tools/aidl.html中對步驟作了詳細描述:

--1.Create your .aidl file - This file defines an interface (YourInterface.aidl) that defines the methods and fields available to a client.
創建你的aidl文件,我在后面給出了一個例子,它的aidl文件定義如下:寫法跟java代碼類似,但是這里有一點值得注意的就是它可以引用其它aidl文件中定義的接口,但是不能夠引用你的java類文件中定義的接口

[java] view plaincopyprint?
  • package?com.cao.android.demos.binder.aidl;????
  • import?com.cao.android.demos.binder.aidl.AIDLActivity;??
    • interface?AIDLService?{?????
    • ????void?registerTestCall(AIDLActivity?cb);?????
    • ????void?invokCallBack();??
    • }????

    package com.cao.android.demos.binder.aidl; import com.cao.android.demos.binder.aidl.AIDLActivity; interface AIDLService { void registerTestCall(AIDLActivity cb); void invokCallBack(); }

    ?

    ?

    --2.Add the .aidl file to your makefile - (the ADT Plugin for Eclipse manages this for you). Android includes the compiler, called AIDL, in the tools/ directory.
    編譯你的aidl文件,這個只要是在eclipse中開發,你的adt插件會像資源文件一樣把aidl文件編譯成java代碼生成在gen文件夾下,不用手動去編譯:編譯生成AIDLService.java如我例子中代碼

    ?


    --3.Implement your interface methods - The AIDL compiler creates an interface in the Java programming language from your AIDL interface. This interface has an inner abstract class named Stub that inherits the interface (and implements a few additional methods necessary for the IPC call). You must create a class that extends YourInterface.Stub and implements the methods you declared in your .aidl file.
    實現你定義aidl接口中的內部抽象類Stub,public static abstract class Stub extends android.os.Binder implements com.cao.android.demos.binder.aidl.AIDLService
    Stub類繼承了Binder,并繼承我們在aidl文件中定義的接口,我們需要實現接口方法,下面是我在例子中實現的Stub類:
    ?

    ?

    [java:showcolumns] view plaincopyprint? ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  • private?final?AIDLService.Stub?mBinder?=?new?AIDLService.Stub()?{??
  • ??
  • ????@Override??
  • ?

    • ????public?void?invokCallBack()?throws?RemoteException?{??
    • ????????Log("AIDLService.invokCallBack");??
    • ????????Rect1?rect?=?new?Rect1();??
    • ????????rect.bottom=-1;??
    • ????????rect.left=-1;??
    • ????????rect.right=1;??
    • ????????rect.top=1;??
    • ????????callback.performAction(rect);??
    • ????}??
    • ??
    • ??
    • ????@Override??
    • ????public?void?registerTestCall(AIDLActivity?cb)?throws?RemoteException?{??
    • ????????Log("AIDLService.registerTestCall");??
    • ????????callback?=?cb;??
    • ????}??
    • };??

    private final AIDLService.Stub mBinder = new AIDLService.Stub() { @Override public void invokCallBack() throws RemoteException { Log("AIDLService.invokCallBack"); Rect1 rect = new Rect1(); rect.bottom=-1; rect.left=-1; rect.right=1; rect.top=1; callback.performAction(rect); } @Override public void registerTestCall(AIDLActivity cb) throws RemoteException { Log("AIDLService.registerTestCall"); callback = cb; } };

    ?

    ?

    ?

    Stub翻譯成中文是存根的意思,注意Stub對象是在被調用端進程,也就是服務端進程,至此,服務端aidl服務端得編碼完成了。

    ?

    --4.Expose your interface to clients - If you're writing a service, you should extend Service and override Service.onBind(Intent) to return an instance of your class that implements your interface.
    第四步告訴你怎么在客戶端如何調用服務端得aidl描述的接口對象,doc只告訴我們需要實現Service.onBind(Intent)方法,該方法會返回一個IBinder對象到客戶端,綁定服務時不是需要一個ServiceConnection對象么,在沒有了解aidl用法前一直不知道它是什么作用,其實他就是用來在客戶端綁定service時接收service返回的IBinder對象的:

    ?

    [java] view plaincopyprint?
  • AIDLService?mService;??
  • private?ServiceConnection?mConnection?=?new?ServiceConnection()?{??
  • ?

    • ????public?void?onServiceConnected(ComponentName?className,?IBinder?service)?{??
    • ????????Log("connect?service");??
    • ????????mService?=?AIDLService.Stub.asInterface(service);??
    • ????????try?{??
    • ????????????mService.registerTestCall(mCallback);??
    • ????????}?catch?(RemoteException?e)?{??
    • ??
    • ????????}??
    • ????}??
    • ??
    • ??
    • ????public?void?onServiceDisconnected(ComponentName?className)?{??
    • ????????Log("disconnect?service");??
    • ????????mService?=?null;??
    • ????}??
    • };??

    AIDLService mService; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { Log("connect service"); mService = AIDLService.Stub.asInterface(service); try { mService.registerTestCall(mCallback); } catch (RemoteException e) { } } public void onServiceDisconnected(ComponentName className) { Log("disconnect service"); mService = null; } };

    ?

    ?

    ?

    ?

    ?

    mService就是AIDLService對象,具體可以看我后面提供的示例代碼,需要注意在客戶端需要存一個服務端實現了的aidl接口描述文件,但是客戶端只是使用該aidl接口,不需要實現它的Stub類,獲取服務端得aidl對象后mService = AIDLService.Stub.asInterface(service);,就可以在客戶端使用它了,對mService對象方法的調用不是在客戶端執行,而是在服務端執行。

    ?

    4.aidl中使用java類,需要實現Parcelable接口,并且在定義類相同包下面對類進行聲明:

    ?

    上面我定義了Rect1類
    之后你就可以在aidl接口中對該類進行使用了
    package com.cao.android.demos.binder.aidl;?
    import com.cao.android.demos.binder.aidl.Rect1;
    interface AIDLActivity {??
    ??? void performAction(in Rect1 rect);??
    }?
    注意in/out的說明,我這里使用了in表示輸入參數,out沒有試過,為什么使用in/out暫時沒有做深入研究。

    ?

    5.aidl使用完整示例,為了清除說明aidl使用,我這里寫了一個例子,例子參考了博客:

    ?

    http://blog.csdn.net/saintswordsman/archive/2010/01/04/5130947.aspx

    ?

    作出說明

    ?

    例子實現了一個AIDLTestActivity,AIDLTestActivity通過bindservice綁定一個服務AIDLTestService,通過并獲取AIDLTestActivity的一個aidl對象AIDLService,該對象提供兩個方法,一個是registerTestCall注冊一個aidl對象,通過該方法,AIDLTestActivity把本身實現的一個aidl對象AIDLActivity傳到AIDLTestService,在AIDLTestService通過操作AIDLActivity這個aidl遠端對象代理,使AIDLTestActivity彈出一個toast,完整例子見我上傳的資源:

    ?

    http://download.csdn.net/source/3284820

    ?

    文章倉促而成,有什么疑問歡迎大家一起討論。

    ?

    轉載于:https://www.cnblogs.com/lz-cswb/archive/2012/04/29/2476349.html

    總結

    以上是生活随笔為你收集整理的android 中的aidl的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。