一種通過AIDL實現DLNA共享服務的方法

作者:Neek.chen


一、AIDL簡述

Android Interfacedefinition language(AIDL),它是一種android內部進程通信接口的描述語言,通過它我們可以定義進程間的通信接口。

首先引用官方文檔一句話:Using AIDL isnecessary only if you allow clients from different applications to access yourservice for IPC and want to handle multithreading in your service. If you donot need to perform concurrent IPC across different applications, you shouldcreate your interface by implementing a Binder or, if you want to perform IPC,but do not need to handle multithreading, implement your interface using aMessenger. Regardless, be sure that you understand Bound Services beforeimplementing an AIDL.

即,只有當你允許來自不同的客戶端訪問你的服務並且需要處理多線程問題時你才必須使用AIDL,其他情況下你都可以選擇其他方法,如使用Messager,也能跨進程通訊。可見AIDL是處理多線程、多客戶端併發訪問的。而Messager是單線程處理。

二、AIDL實例

1. 創建AIDL文件:

寫法跟java代碼類似,但是這裏有一點值得注意的就是它可以引用其它aidl文件中定義的接口,但是不能夠引用你的java類文件中定義的接口。

package com.tpv.xmic.dmc;

import com.tpv.xmic.dmc.dlna.DlnaDeviceInfo;

interface IPushService{    

    int playMedia(String path, String title, int type);

    int stopMedia();

    String getDuration();

    DlnaDeviceInfo getCurrentDMRDevice();

    void setSelectedDMR(in DlnaDeviceInfo info);

}

其中引入一個自定義對象,所以還需要添加相應的AIDL文件。在DlnaDeviceInfo.java的同一個包下添加DlnaDeviceInfo.aidl文件:

package com.tpv.xmic.dmc.dlna;

parcelable DlnaDeviceInfo;

注意到了,要對DLNADeviceInfo對象實現序列化(Parcelable),序列化方法不在這討論了。可參考:http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html

 

2. 如果AIDL文件的內容是正確的,ADT會自動在gen目錄下生成一個Java接口文件(IPushService.java)。

3. 建立一個服務類 PushService,實現由AIDL文件生成的Java接口:

public class PushService extends Service{

       private final IPushService.Stub mBinder = new IPushService.Stub() {

              @Override

              public int playMedia(String path, String title, int type) throws RemoteException {

                     return 0;

              }

              @Override

              public int stopMedia() throws RemoteException {

                     Log.v(TAG, "stopMedia()");

                     return 0;

              }

              @Override

              public String getDuration() throws RemoteException {

                     Log.v(TAG, "getDuration()");

                     return null;

              }

              @Override

              public List<DlnaDeviceInfo> getDMRDevicesList() throws RemoteException {

                     Log.i(TAG , "getDMRDevicesList() ");

                     return null;

              }

              @Override

              public void setSelectedDMR(DlnaDeviceInfo info) throws RemoteException {

                     Log.i(TAG , "setSelectedDMR()==> DMR: " +(info==null? "NULL":info.Name));

              }

       };

      

       @Override

       public IBinder onBind(Intent intent) {

              Log.i(TAG, "onBind()");

              return mBinder;

       }

       ……

}

 

4. 在AndroidManifest.xml文件中配置AIDL服務

尤其要注意的是,<action>標籤中android:name的屬性值就是客戶端要引用該服務的ID,也就是Intent類的參數值。

……

<service

      android:name=".PushService"

      android:exported="true" >

      <intent-filter>

          <action android:name="com.tpv.xmic.dmc.PushService" />

      </intent-filter>

</service>

……

 

5. 客戶端導入

1)    導入AIDL文件,放在對應的包名下。包括:IpushService.aidl,DlnaDeviceInfo.aidl

2)  添加對象:

private IPushService mService;

private ServiceConnection mMRconnection = new ServiceConnection() {

       @Override

       public void onServiceDisconnected(ComponentName arg0) {

              mService = null;

       }

       @Override

       public void onServiceConnected(ComponentName name, IBinder service) {

        // 獲得Service對象

              mService = IPushService.Stub.asInterface(service);

       }

};

3) 綁定服務:

Intent intent = new Intent("com.tpv.xmic.dmc.PushService");

intent.setClassName("com.tpv.xmic.dmc", "com.tpv.xmic.dmc.PushService");

startService(intent);

bindService(intent,   mMRconnection, Context.BIND_AUTO_CREATE);

 

6. 調用接口

服務綁定成功後,就可以調用mService的接口了。盡情玩耍吧!

 

 

引用查詢資料出處

http://www.2cto.com/kf/201406/312244.html

http://www.cnblogs.com/renqingping/archive/2012/10/25/Parcelable.html

 


發佈了60 篇原創文章 · 獲贊 24 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章