当前位置:操作系统 > 安卓/Android >>

Android AIDL 远程服务器使用示例

 

很多网友来函表示对Android AIDL不是很理解,这里Android123准备了一个简单的例子,帮助大家了解Android上比较强大的远程服务设计吧。

 

 

一、为什么要使用AIDL,他的优势有哪些呢?

    AIDL服务更像是 一个Server,可以为多个应用提供服务。由于使用了IDL这样类似COM组件或者说中间语言的设计,可以让后续的开发者无需了解内部,根据暴漏的接口实现相关的操作,AIDL可以工作在独立的进程中。

 

 

二、学习AIDL服务需要有哪些前置知识?

    作为Android上服务的扩展,首先你要了解Android Service模型,Android Serivice我们可以分为两种模式,三个类型,1.最简单的Service就是无需处理onBind方法,一般使用广播通讯,效率比较低。2. 使用Binder模式处理Service和其他组件比如Activity通讯,Android开发网提示对于了解了Binder模式的服务后,开发AIDL远程服务就轻松容易的多。

 

 

三、具体实例

    我们以com.android123.cwj.demo为工程名,首先在工程目录的com.android123.cwj目录下创建一个ICWJ.aidl文件,内容为

view sourceprint?1 package com.android123.cwj; 

 

2   interface ICWJ { 

 

3   String getName(); 

 

4 }

 

  如果格式AIDL格式没有问题,在Eclipse中ADT插件会在工程的gen目录下会自动生成一个Java实现文件。

 

 

在Service中代码如下:

view sourceprint?01 public class CWJService extends Service { 

 

02 public static final String BIND = "com.android123.cwj.CWJService.BIND";   

 

03 public String mName="android123"; 

 

04 private final ICWJ.Stub mBinder = new ICWJ.Stub() 

 

05   { 

 

06      @Override 

 

07      public String getName() throws RemoteException 

 

08      {    //重写在AIDL接口中定义的getName方法,返回一个值为我们的成员变量mName的内容。 

 

09         try

 

10         { 

 

11           return CWJService.this.mName; 

 

12         } 

 

13         catch(Exception e) 

 

14         { 

 

15            return null; 

 

16         } 

 

17     } 

 

18 }; 

 

19   

 

20   @Override

 

21   public void onCreate() { 

 

22    Log.i("svc", "created.");  

 

23   } 

 

24   

 

25   @Override

 

26   public IBinder onBind(Intent intent) { 

 

27      return mBinder;    //这里我们要返回mBinder而不是一般Service中的null 

 

28   } 

 

29   

 

30 }

 

接着在AndroidManifest.xml文件中定义 

view sourceprint?1 <service android:name=".CWJService"> 

 

2    <intent-filter> 

 

3     <action android:name="com.android123.cwj.CWJService.BIND" /> 

 

4     <category android:name="android.intent.category.DEFAULT"/> 

 

5    </intent-filter> 

 

6   </service>

 

接下来在Activity中的调用,我们可以

view sourceprint?01 private ICWJ objCWJ = null; 

 

02    private ServiceConnection serviceConn = new ServiceConnection() { 

 

03   

 

04   @Override

 

05   public void onServiceDisconnected(ComponentName name) { 

 

06    } 

 

07   

 

08   @Override

 

09   public void onServiceConnected(ComponentName name, IBinder service) { 

 

10    objCWJ = ICWJ.Stub.asInterface(service); 

 

11   } 

 

12 };

 

在Activity的onCreate中加入绑定服务的代码

view sourceprint?1 Intent intent = new Intent(); 

 

2   intent.setClass(this, CWJService.class); 

 

3   bindService (intent, serviceConn, Context.BIND_AUTO_CREATE);

 

同时重写Activity的onDestory方法

view sourceprint?1 @Override

 

2    public void onDestroy() { 

 

3         unbindService(serviceConn);  // 取消绑定 

 

4     super.onDestroy(); 

 

5    }

 

执行AIDL的getName可以通过下面的方法

view sourceprint?1 if (objCWJ == null) 

 

2    { 

 

3        try { 

 

4             String strName = obj.getName(); 

 

5        }  

 

6        catch (RemoteException e)  

 

7        {} 

 

8    }

 

以上过程中,如果ADT插件没有自动生成ICWJStub类在工程的gen目录下时,可以手动在sdk根目录下platform-tools目录下,手动实用AIDL.exe来生成,这样可以看清楚AIDL文件到底是哪里存在格式上的错误。

 

 

 

 

注:本文来源于网络,具体出处无从考察。

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,