<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                阿斗(aidl諧音)本來是扶不起的,可是我們有了AIDL工具,就有可能將他扶起! 1. 我能干什么 在Java層中,如果想要利用Binder進行跨進程的通信,也得定義一個類似ITest的接口,不過這是一個aidl文件。現假設,服務端程序都在com.test.service包中。 ITest.aidl文件內容如下: **ITest.aidl** ~~~ package com.test.service; import com.test.complicatedDataStructure interface ITest{ // complicatedDataStructure類是自己定義的復雜數據結構,in表示輸入參數,out表示輸出參數 //in和out的表示一定要準確。切記! int getTest(outcomplicatedDataStructure); int setTest(inString name,in boolean reStartServer); } ~~~ 定義完后,如果用Eclipse進行編譯,會在gen目錄下生成一個com.test.ITest.java文件(也會生成對應包結構的目錄)。內容就不具體羅列了,只關注其是如何實現服務端的。 說明:Eclipse用的也是aidl工具,可以手動使用這個工具編譯aidl文件。 2. 實現服務端 com.test.ITest.java只是實現了一個類似BnTest的東西,具體的業務實現還需從ITest.Stub派生,實現代碼如下所示: **ITestImpl.java** ~~~ /* ITest.Stub是在aidl生成的那個java文件中定義的,非常類似Native層的BnTest,。 ITestImpl必須從ITest.Stub中派生,用來實現具體的業務函數。 */ package com.test.service class ITestImpl extends ITest.Stub{ public void getTest(complicatedDataStructurecds) throws RemoteException { //在這里實現具體的getTest } public voidsetTest(in String name,in boolean reStartServer) throwsRemoteException { //在這里實現具體的setTest } } ~~~ 這時,你的Eclipse下會有如下兩個目錄: - src下有一個com.test.service包結構目錄。 - gen下也有一個com.test.service包結構目錄,其中的內容是由aidl工具生成的。 3. 實現代理端 代理端往往在另外一個程序中使用。假設是com.test.client包,把剛才com.test.service工程的gen下com.test.service目錄全部復制到com.test.client中。這樣,client工程也有兩個包結構目錄: - com.test.client。 - com.test.service。不過這個目錄僅僅只有aidl生成的Java文件。 服務端一般駐留于Service進程,所以可以在Client端的onServiceConnected函數中獲得代理對象,實現代碼如下所示: **Client端示例代碼** ~~~ //不一定是在onServiceConnected中,但它是比較合適的。 private ServiceConnection serviceConnection = newServiceConnection() { //@Override public void onServiceConnected(ComponentName name, IBinder service) { if(ITestProxy== null) ITestProxy = ITest.Stub.asInterface(service);//這樣你就得到BpTest了 } ~~~ 4. 傳遞復雜的數據結構 AIDL支持簡單數據結構與Java 中String類型的數據進行跨進程傳遞,如果想做到跨進程傳遞復雜的數據結構,還須另做一些工作。 以ITest.aidl文件中使用的complicatedDataStructure為例: * 它必須實現implements Parcelable接口。 * 內部必須有一個靜態的CREATOR類。 * 定義一個complicatedDataStructure.aidl文件 * * * * * **說明**:可參考Android API文檔的parcelable類,里邊有一個很簡單的例子。 * * * * * 來看這個Java文件的實現: **complicatedDataStructure.java** ~~~ package com.test.service; import android.os.Parcel; import android.os.Parcelable; public class complicatedDataStructure implementsParcelable { publicstatic final int foo1 = 0; publicstatic final int foo2 = 1; publicString fooString1 = null; publicString fooString2 = null; //靜態Creator類 public static final Parcelable.Creator< complicatedDataStructure> CREATOR = newParcelable.Creator< complicatedDataStructure >() { public complicatedDataStructure createFromParcel(Parcel in) { return new complicatedDataStructure (in); } public complicatedDataStructure [] newArray(int size) { return new complicatedDataStructure [size];//用于傳遞數組 } }; public complicatedDataStructure(complicatedDataStructureother) { fooString1= other. fooString1; fooString2= other. fooString2; foo1= other. foo1; foo2 =other. foo2; } private complicatedDataStructure(Parcel in) { readFromParcel(in); } /*@Override */ publicint describeContents() { return 0; } publicvoid readFromParcel(Parcel in) { foo1= in. readInt (); foo2= in. readInt (); fooString1= in.readString(); fooString2= in.readString(); } /*@Override */ publicvoid writeToParcel(Parcel dest, int flags) { dest.writeInt(foo1); dest.writeInt(foo2); dest.writeString(fooString1); dest.writeString(fooString2); } } ~~~ complicatedDataStructure.aidl該怎么寫呢?如下所示: **complicatedDataStructure.aidl** ~~~ package com.test.service; parcelable complicatedDataStructure; ~~~ 然后,在使用它的aidl文件中添加下行代碼即可: ~~~ import com.test.complicatedDataStructure ~~~ 有了AIDL,再看我們的阿斗是不是能扶得起了呢?當然,想讓上面的程序正確工作,還得再努把力,把未盡的業務層事業完成。另外,還要經得起殘酷環境的考驗(即通過測試來檢驗自己的程序)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看