徹底明確Android中AIDL及其使用

來源:互聯網
上載者:User

標籤:roc   列印   begin   access   oid   connect   學什麼   dem   roi   

1、為什麼要有AIDL?不管學什麼東西,最先得弄明確為什麼要有這個東西。不要說存在即是合理。存在肯定合理,可是你還是沒有明確。

對於AIDL有一些人的淺顯概念就是,AIDL能夠跨進程訪問其它應用程式,和其它應用程式通訊,那我告訴你。非常多技術都能夠訪問,如廣播(應用A在AndroidManifest.xml中注冊指定Action的廣播)應用B發送指定Action的廣播,A就能收到資訊,這樣也能看成不同應用之間完畢了通訊(可是這樣的通訊是單向的)。還如ContentProvider,通過URI介面暴露資料給其它應用訪問;可是這樣的都算不上是應用之間的通訊。

可能最讓人迷惑的是Android推出來了Messager,它就是完畢應用之間的通訊的。那麼為什麼還要有AIDL呢。官方文檔介紹AIDL中有這麼一句話:

Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.
第一句最重要,“僅僅有當你同意來自不同的client訪問你的服務而且須要處理多線程問題時你才必須使用AIDL”。其它情況下你都能夠選擇其它方法。如使用Messager,也能跨進程通訊。

可見AIDL是處理多線程、多client並發訪問的。而Messager是單線程處理。還是官方文檔說的明確,一句話就能夠理解為什麼要有AIDL。那麼是不是這樣的寫個AIDL試試。

2、AIDL使用第一、定義AIDL檔案
// IRemoteService.aidlpackage com.example.android;// Declare any non-default types here with import statements/** Example service interface */interface IRemoteService {    /** Request the process ID of this service, to do evil things with it. */    int getPid();    /** 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);}
這段代碼也是官方文檔的。

命名為IRemoteService.aidl。放在com.example.android包下(這個能夠任意),儲存後Android編譯器會在gen檔案夾下自己主動產生IRemoteService.java檔案

第二、定義我們的服務,DDService.java,而且須要在AndroidManifest.xml中注冊,並加入“duanqing.test.aidl” 的ACTION
package com.example.service;import com.example.android.IRemoteService;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.Process;public class DDService extends Service {@Overridepublic void onCreate() {super.onCreate();System.out.println("DDService onCreate........" + "Thread: " + Thread.currentThread().getName());}@Overridepublic IBinder onBind(Intent arg0) {System.out.println("DDService onBind");return mBinder;}private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {    public int getPid(){    System.out.println("Thread: " + Thread.currentThread().getName());    System.out.println("DDService getPid ");        return Process.myPid();    }    public void basicTypes(int anInt, long aLong, boolean aBoolean,        float aFloat, double aDouble, String aString) {    System.out.println("Thread: " + Thread.currentThread().getName());    System.out.println("basicTypes aDouble: " + aDouble +" anInt: " + anInt+" aBoolean " + aBoolean+" aString " + aString);    }};}

這樣我們的服務端就完畢了。把服務端執行到模擬器(或者手機上)。等一會能夠看一下資訊列印,重點看“線程名”
第三、實現client測試代碼建立還有一個project,相同須要加入AIDL協議檔案(這是一個標準的協議檔案。定義對外服務),這裡我列出來我的測試代碼:
package com.example.aidlclient;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.Process;import android.os.RemoteException;import android.view.View;import com.example.android.IRemoteService;public class MainActivity extends Activity {private IRemoteService remoteService;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }        ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {remoteService =IRemoteService.Stub.asInterface(service);try {int pid = remoteService.getPid();int currentPid = Process.myPid();System.out.println("currentPID: " + currentPid +"  remotePID: " + pid);remoteService.basicTypes(12, 1223, true, 12.2f, 12.3, "我們的愛,我明確");} catch (RemoteException e) {e.printStackTrace();}System.out.println("bind success! " + remoteService.toString());}};    /**     * 監聽按鈕點擊     * @param view     */    public void buttonClick(View view) {    System.out.println("begin bindService");    Intent intent = new Intent("duanqing.test.aidl");    bindService(intent, conn, Context.BIND_AUTO_CREATE);    }    @Override    protected void onDestroy() {    super.onDestroy();    unbindService(conn);    }}
4、運行點擊clientbutton,運行。看資訊列印:
看服務端列印,DDService onCreate..........Thread: main,主線程,當client調用服務端getPid方法時,服務端是在Thread: Binder2中運行,當client調用服務端basicType方法時,服務端是在Thread:Binder1中運行

徹底明確Android中AIDL及其使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.