<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                在Android中,每個應用程序都有自己的進程,當需要在不同的進程之間傳遞對象時,該如何實現呢?顯然,Java中是不支持跨進程內存共享的,因此要傳遞對象,需要把對象解析成操作系統能夠理解的數據格式,以達到跨界對象訪問的目的。在Android中,則采用AIDL(Android Interface Definition Language :接口定義語言)方式實現。 AIDL (Android Interface Definition Language)是一種IDL語言,用于生成可以在Android設備上兩個進程之間進行進程間通信(IPC)的代碼。如果在一個進程中(例如Activity)要調用另一個進程中(例如Service)對象的操作,就可以使用AIDL生成可序列化的參數。 AIDL IPC機制是面向接口的,它是使用代理類在客戶端和實現端傳遞數據。 **使用AIDL實現IPC** 使用AIDL實現IPC服務的步驟是: 1.創建.aidl文件-該文件(YourInterface.aidl)定義了客戶端可用的方法和數據的接口。 2.在makefile文件中加入.aidl文件-(Eclipse中的ADT插件提供管理功能)Android包括名為AIDL的編譯器,位于tools/文件夾。 3.實現接口-AIDL編譯器從AIDL接口文件中利用Java語言創建接口,該接口有一個繼承的命名為Stub的內部抽象類(并且實現了一些IPC調用的附加方法),要做的就是創建一個繼承于YourInterface.Stub的類并且實現在.aidl文件中聲明的方法。 4.向客戶端公開接口-如果是編寫服務,應該繼承Service并且重載Service.onBind(Intent)以返回實現了接口的對象實例 **創建.aidl文件** AIDL使用簡單的語法來聲明接口,描述其方法以及方法的參數和返回值。這些參數和返回值可以是任何類型,甚至是其他AIDL生成的接口。重要的是必須導入所有非內置類型,哪怕是這些類型是在與接口相同的包中。下面是AIDL能支持的數據類型: 1.Java編程語言的主要類型 (int, boolean等)—不需要 import語句。 2.以下的類 (不需要import語句): **String** **List**-列表中的所有元素必須是在此列出的類型,包括其他AIDL生成的接口和可打包類型。List可以像一般的類(例如List<String>)那樣使用,另一邊接收的具體類一般是一個ArrayList,這些方法會使用List接口。 **Map**- Map中的所有元素必須是在此列出的類型,包括其他AIDL生成的接口和可打包類型。一般的maps(例如Map<String,Integer>)不被支持,另一邊接收的具體類一般是一個HashMap,這些方法會使用Map接口。 **CharSequence**-該類是被TextView和其他控件對象使用的字符序列。 3.通常引引用方式傳遞的其他AIDL生成的接口,必須要import語句聲明 4.實現了Parcelable protocol以及按值傳遞的自定義類,必須要import 語句聲明。 通過對上面的基本了解,下面我就以一個具體的實例來說明Android中如何通過AIDL機制來實現兩個進程中實現通訊:(情景假設:例如**A應用**通過服務Service方式向**B應用**提供通過書籍編號來查詢書籍名稱的服務) **A應用程序結構圖如下:** ![](https://box.kancloud.cn/2016-05-16_57397391a31b1.png) 通過上面的結構圖可以看到,在A應用程序中創建一個 aidl 的接口,然后系統在 gen 目錄下自動生成相應的 java 文件。 其中 IBook.aidl 文件的源碼: ~~~ package com.andyidea.aidl; interface IBook { String queryBook(int bookNo); } ~~~ 其中 BookService.java 類中的源碼如下: ~~~ package com.andyidea.service; import com.andyidea.aidl.IBook; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; /** * 查詢書籍的服務 * @author Andy * */ public class BookService extends Service { private String[] bookNames = {"Java編程思想","設計模式","Android開發設計"}; private IBinder mIBinder = new BookBinder(); @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return mIBinder; } /** * 服務中交互的方法 * @param bookNo * @return */ public String queryBookName(int bookNo){ if(bookNo > 0 && bookNo <= bookNames.length){ return bookNames[bookNo-1]; } return null; } private class BookBinder extends IBook.Stub{ @Override public String queryBook(int bookNo) throws RemoteException { return queryBookName(bookNo); } } } ~~~ 同時別忘了在 Manifest.xml中配置該服務對象(標紅色的部分),建議采用隱式方式激活該服務,適合不同的進程的意圖。 ~~~ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.andyidea.service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <service android:name=".BookService"> <intent-filter> <action android:name="com.andyidea.aidl.bookservice"/> </intent-filter> </service> </application> </manifest> ~~~ 以上我們已經實現了A應用程序提供服務的功能,下面我們來實現B應用(或者其它需要用到A應用提供服務的應用程序) **B應用程序結構圖如下:** **![](https://box.kancloud.cn/2016-05-16_57397391b8138.png)** 我們看到B應用程序也要和服務端同樣的 .aidl 文件,我們可以從A應用程序中把該 aidl 文件中拷貝過來就是了,呵。由于B應用中 .aidl 文件和 A應用中的 .aidl 文件源碼一樣,我在這里就不列出來了。 其中AIDLClientDemoActivity.java源碼如下:【注:其中該客戶端類要通過 bindService 方式來啟動另外一個進程的服務,這樣才能實現和服務進行交互。如果通過startService方式來啟動服務,則不能與服務進行交互】 ~~~ package com.andyidea.client; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.andyidea.aidl.IBook; public class AIDLClientDemoActivity extends Activity { private EditText numberText; private TextView resultView; private Button query; private IBook bookQuery; private BookConnection bookConn = new BookConnection(); /**Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); numberText = (EditText) this.findViewById(R.id.number); resultView = (TextView) this.findViewById(R.id.resultView); query = (Button)findViewById(R.id.query); Intent service = new Intent("com.andyidea.aidl.bookservice"); bindService(service, bookConn, BIND_AUTO_CREATE); query.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String number = numberText.getText().toString(); int num = Integer.valueOf(number); try { resultView.setText(bookQuery.queryBook(num)); } catch (RemoteException e) { e.printStackTrace(); } } }); } @Override protected void onDestroy() { unbindService(bookConn); super.onDestroy(); } private final class BookConnection implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub bookQuery = IBook.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub bookQuery = null; } } } ~~~ 其中界面布局文件 main.xml 源碼: ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="書籍編號" /> <EditText android:id="@+id/number" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/query" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查詢"/> <TextView android:id="@+id/resultView" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> ~~~ 至此,我們已經完成了B應用程序的代碼實現部分,我們要先把A應用程序部署到機器上,然后我們再運行B應用程序。下面我們通過截圖來看下程序運行的結果: ![](https://box.kancloud.cn/2016-05-16_57397391cd6ff.png) 通過上面的截圖,我們輸入書籍編號 1,就可以查詢出相應的書籍名稱,到此,我們就可以了解了 Android應用中如何通過AIDL機制實現兩個進程的通訊。 注:本文為 Andy.Chen 原創,歡迎大家轉載,轉載請大家注明出處,謝謝。
                  <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>

                              哎呀哎呀视频在线观看