Android AIDL實例解析


AIDL這項技術在我們的開發中一般來說並不是很常用,雖然自己也使用新浪微博的SSO登錄,其原理就是使用AIDL,但是自己一直沒有動手完整的寫過AIDL的例子,所以就有了這篇簡單的文章。
AIDL(AndRoid接口描述語言)是一種借口描述語言; 編譯器可以通過aidl文件生成一段代碼,通過預先定義的接口達到兩個進程內部通信進程的目的. 如果需要在一個Activity中, 訪問另一個Service中的某個對象, 需要先將對象轉化成 AIDL可識別的參數(可能是多個參數), 然后使用AIDL來傳遞這些參數, 在消息的接收端, 使用這些參數組裝成自己需要的對象.
說白了,AIDL就是定義一個接口,客戶端(調用端)通過bindService來與遠程服務端簡歷一個連接,在該連接建立時會將返回一個IBinder對象,該對象是服務端Binder的BinderProxy,在建立連接時,客戶端通過asInterface函數將該BinderProxy對象包裝成本地的Proxy,並將遠程服務端的BinderProxy對象賦值給Proxy類的mRemote字段,就是通過mRemote執行遠程方法調用。需要對Binder機制有更深的理解,請轉到老羅的系列文字吧,Android系統進程間通信Binder機制在應用程序框架層的Java接口源代碼分析。下面我們看一個AIDL實例。


AIDL接口聲明
在src目錄下創建一個com.example.advanceandroid.aidl包,然后在該包下創建一個ILogin.aidl文件,注意是創建文件而不是類或者接口類型。在ILogin.aidl中聲明接口,實例如下 :

package com.example.advanceandroid.aidl;

interface ILogin {
String login();
}

注意看,接口和方法聲明都不用public,方法加入public會提示錯誤。編寫完后如果eclipse開啟了自動編譯則會在gen/com.example.advanceandroid.aidl下生成一個ILogin.java類,內容大致如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.example.advanceandroid.aidl;
 
public interface ILogin extends android.os.IInterface
{
     /** Local-side IPC implementation stub class. */
     public static abstract class Stub extends android.os.Binder implements
             com.example.advanceandroid.aidl.ILogin
     {
         private static final java.lang.String DESCRIPTOR = "com.example.advanceandroid.aidl.ILogin" ;
 
         /** Construct the stub at attach it to the interface. */
         public Stub()
         {
             this .attachInterface( this , DESCRIPTOR);
         }
 
         /**
          * Cast an IBinder object into an com.example.advanceandroid.aidl.ILogin
          * interface, generating a proxy if needed.
          */
         public static com.example.advanceandroid.aidl.ILogin asInterface(android.os.IBinder obj)
         {
             if ((obj == null )) {
                 return null ;
             }
             android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
             if (((iin != null ) && (iin instanceof com.example.advanceandroid.aidl.ILogin))) {
                 return ((com.example.advanceandroid.aidl.ILogin) iin);
             }
             return new com.example.advanceandroid.aidl.ILogin.Stub.Proxy(obj);
         }
 
         @Override
         public android.os.IBinder asBinder()
         {
             return this ;
         }
 
         @Override
         public boolean onTransact( int code, android.os.Parcel data, android.os.Parcel reply,
                 int flags) throws android.os.RemoteException
         {
             switch (code)
             {
                 case INTERFACE_TRANSACTION: {
                     reply.writeString(DESCRIPTOR);
                     return true ;
                 }
                 case TRANSACTION_login: {                            // 1、登錄請求,執行的是this.login();
                     data.enforceInterface(DESCRIPTOR);
                     java.lang.String _result = this .login();
                     reply.writeNoException();
                     reply.writeString(_result);
                     return true ;
                 }
             }
             return super .onTransact(code, data, reply, flags);
         }
 
         private static class Proxy implements com.example.advanceandroid.aidl.ILogin
         {
             private android.os.IBinder mRemote;
 
             Proxy(android.os.IBinder remote)
             {
                 mRemote = remote;
             }
 
             @Override
             public android.os.IBinder asBinder()
             {
                 return mRemote;
             }
 
             public java.lang.String getInterfaceDescriptor()
             {
                 return DESCRIPTOR;
             }
 
             @Override
             public java.lang.String login() throws android.os.RemoteException                // 2、Proxy中的login,通過Binder機制實現IPC
             {
                 android.os.Parcel _data = android.os.Parcel.obtain();
                 android.os.Parcel _reply = android.os.Parcel.obtain();
                 java.lang.String _result;
                 try {
                     _data.writeInterfaceToken(DESCRIPTOR);
                     mRemote.transact(Stub.TRANSACTION_login, _data, _reply, 0 );
                     _reply.readException();
                     _result = _reply.readString();
                 } finally {
                     _reply.recycle();
                     _data.recycle();
                 }
                 return _result;
             }
         }
 
         static final int TRANSACTION_login = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0 );
     }
 
     public java.lang.String login() throws android.os.RemoteException;
}

可以看到,該類中自動生成了ILogin接口,該接口中又一個login()函數。但最重要的是里面生成了一個Stub類,該類集成子Binder類,並且實現了ILogin接口。Stub里面最重要的就是asInterface()這個函數,在這個函數中會判斷obj參數的類型,如果是該obj是本地的接口類似,則認為不是IPC,會將該obj轉換成ILogin類型;否則會通過自動生成的另一個內部類Proxy來包裝obj,將其賦值給Proxy中的mRemote屬性。Proxy類也實現了ILogin接口,在login()函數中,Proxy將通過Binder機制向服務端傳遞請求和數據,如上面代碼中的注釋2。這是客戶端的工作算是完成了。

服務端AIDL接口
服務端也需要在相同的包下創建同名的aidl文件,我們直接將客戶端的com.example.advanceandroid.aidl包下的ILogin.aidl拷貝到服務端即可,如果用到了自定義的類型,那么該自定義類型也需要在客戶端、服務端都有。拷貝完aidl后,在服務端程序中也會在gen中生成對應的ILogin.java文件,內容同客戶端一樣。這里的重點我們要看onTransact函數,即上述代碼中的注釋1處,可以看到,在case TRANSACTION_login處執行了this.login()函數,意思是當接收到客戶端的TRANSACTION_login請求時,執行this.login()函數,通過客戶端的分析我們知道,當我們調用login()時實際上就是通過mRemote向服務端提交了一個TRANSACTION_login請求,因此就兩端通過Binder機制就對接上了,我們可以簡單的理解為C/S模式。

服務端還沒有完,最重要的一步時建立一個Service,內容大致如下 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* AIDL服務端接口,LoginStubImpl實現了ILogin接口.
*
* @author mrsimple
*/
public class LoginService extends Service {
 
     /**
      *
      */
     IBinder mBinder = new LoginStubImpl();
 
     /**
      * @author mrsimple
      */
     class LoginStubImpl extends Stub {
         @Override
         public String login() throws RemoteException {
             return "這是從 " + this .getClass().getName() + " 返回的字符串" ;
         }
     }
 
     /*
      * 返回Binder實例,即實現了ILogin接口的Stub的子類,這里為LoginStubImpl
      * [url=home.php?mod=space&uid=133757]@see[/url] android.app.Service#onBind(android.content.Intent)
      */
     @Override
     public IBinder onBind(Intent intent) {
         return mBinder;
     }
}

該Service我們這里命名為LoginService,繼承自Service,然后建一個名為LoginServiceImpl的內部類,該類繼承自自動生成的Stub,然后實現login()方法。在LoginService中聲明一個IBinder字段mBinder :

IBinder mBinder = new LoginStubImpl();

並且在LoginService的onBind函數中將mBinder對象返回。即在客戶端建立與服務端的連接時,會調用onBind方法將mBinder對象返回,在客戶端的ServiceConnection類的onServiceConnected函數中得到的對象IBinder就是經過BinderProxy包裝的LoginService中的mBinder對象。因此在服務端中的onTransact中調用的this.login()函數實際上就是調用的LoginStubImpl中的login()函數。

在服務端程序的AndroidManifest.xml中注冊LoginService,如下 :

?
1
2
3
4
5
6
<!-- aidl server service -->
     <service android:name= "com.example.advanceandroid.aidl.LoginService" >
         <intent-filter>
             
         </action></intent-filter>
     </service>


客戶端建立連接

在Activity中加入如下代碼 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
ServiceConnection mLoginConnection = new ServiceConnection() {
 
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d( "" , "### aidl disconnected." );
        }
 
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d( "" , "### aidl onServiceConnected.     service : " + service.getClass().getName());
 
            ILogin login = Stub.asInterface(service);
            Log.d( "" , "### after asInterface : " + login.getClass().getName());
            try {
                Log.d( "" , "### login : " + login.login());
                // Toast.makeText(MainActivity.this, "onServiceConnected : " +
                // login.login(),
                // Toast.LENGTH_SHORT).show();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };
 
    @Override
    protected void onResume() {
        super .onResume();
 
        // 服務端的action
        Intent aidlIntent = new Intent( "com.example.advanceandroid.aidl.LoginService" );
        bindService(aidlIntent, mLoginConnection, Context.BIND_AUTO_CREATE);
    }
 
    @Override
    protected void onStop() {
        super .onStop();
        // unbind
        unbindService(mLoginConnection);
    }

運行

先運行服務端程序,然后在啟動客戶端程序,可以看到客戶端輸出如下Log:

?
1
2
3
09 - 02 10 : 40 : 54.662 : D/( 9589 ): ### aidl onServiceConnected.     service : android.os.BinderProxy
09 - 02 10 : 40 : 54.662 : D/( 9589 ): ### after asInterface : com.example.advanceandroid.aidl.ILogin$Stub$Proxy
09 - 02 10 : 40 : 54.662 : D/( 9589 ): ### login : 這是從 com.example.advanceandroid.aidl.LoginService$LoginStubImpl 返回的字符串

可以看淡onServiceConnected(ComponentName name, IBinder service)中的service對象是BinderProxy類型,經過asInterface轉換后被包裝成了Proxy類型,但是調用的時候,執行的是服務端LoginStubImpl中的login()函數。因此,LoginStubImpl實例mBinder被服務端包裝成BinderProxy類型,再經過客戶端的Proxy進行包裝,通過Binder機制進行數據傳輸,實現IPC。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM