一种通过AIDL实现DLNA共享服务的方法

来源:互联网 发布:linux下如何删除用户 编辑:程序博客网 时间:2024/05/01 07:44

作者: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

 


0 0