Android中AIDL的简单使用

移动开发 Android
本文通过一个Android中AIDL与service通信的小demo,让刚刚接触这方面的初学者能够对AIDL的简单使用有一个入门级别的的认识。

AIDL这里就不加累述它的概念定义等等,免得长篇大幅。下面介绍的是简单的一次使用AIDL成功与service通信的一个例子:

1.在项目包下新建一个IInfo.aidl,并在其中添加你要调用的方法,格式和java中接口一样。package com.android.server;

  1. interface IInfo {  
  2.     boolean start(); 
  3.     void stop(); 
  4.     void locate(int x, int y); 
  5.     void move(int dx, int dy); 
  6.     void getLocation(inout int[] p);//参数为数组的话,可以加上inout,不然会报错 
  7.     void setTimeout(int t); 
  8.     int getTimeout(); 
  9.     void setBitmap(inout byte[] bmp, int width, int height);}   

正确写好之后,eclipse的adt会自动在gen目录下生成一个IInfo.java文件

2.新建一个CursorService.java类,继承IInfo.stub,如下:

  1. package com.android.server; 
  2. public class CursorService extends ICursorInfo.Stub{ 
  3.     final boolean hasService; 
  4.     public CursorService() { 
  5.         hasService = initializeJNI(); 
  6.     } 
  7.     public synchronized boolean start() { 
  8.         if (hasService) 
  9.             return start0(); 
  10.         return false
  11.     } 
  12.     public synchronized void stop() { 
  13.         if (hasService) 
  14.             stop0(); 
  15.     } 
  16.     public synchronized void locate(int x, int y) { 
  17.         if (hasService) 
  18.             locate0(x, y); 
  19.     } 
  20.   
  21.     public synchronized void move(int dx, int dy) { 
  22.        if (hasService) 
  23.            move0(dx, dy); 
  24.     } 
  25.   
  26.     public synchronized void getLocation(int[] p) { 
  27.         if (p == null
  28.             throw new NullPointerException("p is null"); 
  29.         if (p.length < 2
  30.             throw new IllegalArgumentException("p.len must >= 2"); 
  31.         if (hasService) 
  32.             getPosition0(p); 
  33.     } 
  34.     public synchronized void setTimeout(int t) { 
  35.         if (hasService) 
  36.             setTimeout0(t); 
  37.     } 
  38.   
  39.     public synchronized int getTimeout() { 
  40.         if (hasService) 
  41.             return getTimeout0(); 
  42.         return -1
  43.     } 
  44.   
  45.     public void setBitmap(byte[] bmp, int width, int height) { 
  46.         if(bmp == null
  47.             throw new NullPointerException("bmp is null"); 
  48.         if(width < 0 || height < 0
  49.            throw new IllegalArgumentException("width < 0 || height < 0"); 
  50.        if(width * height > bmp.length) 
  51.             throw new IndexOutOfBoundsException("bmp less than width*height"); 
  52.         setBitmap0(bmp,width,height); 
  53.     } 

在其中实现你aAIDL中的方法

3. 新建一个Manager类,在其中构造一个内部服务连接类,实现ServiceConnection接口:

  1. public class Manager { 
  2.     private static final String TAG = "Manager"
  3.     private IInfo   iCurSer; 
  4.     private Manager(){ 
  5.     } 
  6.       
  7.     public Manager(Context ctx){ 
  8.         this.context = ctx; 
  9.         new Manager(); 
  10.     } 
  11.       
  12.        /**这里就可以与service正常通信,调用service中的方法**/ 
  13.     public void startService(){ 
  14.         Intent intent=new Intent("com.android.server.CursorService"); 
  15.         context.bindService(intent,new CursorServiceConnection(), 
  16.                 Service.BIND_AUTO_CREATE); 
  17.     } 
  18.     /** 
  19.      * 实现ServiceConnection接口 
  20.      * */ 
  21.     public final class CursorServiceConnection implements ServiceConnection{ 
  22.        // 和CursorService绑定时系统回调这个方法 
  23.         @Override 
  24.         public void onServiceConnected(ComponentName name, IBinder service) { 
  25.            // 此处不能使用强制转换, 应该调用Stub类的静态方法获得CursorService接口的实例对象 
  26.            iCurSer=ICursorInfo.Stub.asInterface(service); 
  27.         } 
  28.   
  29.         //解除和CursorService的绑定时系统回调这个方法 
  30.         @Override 
  31.         public void onServiceDisconnected(ComponentName name) { 
  32.             iCurSer=null
  33.         } 
  34.     } 

Android中AIDL的简单使用就mark到这吧,希望对刚刚学Android的开发者能有些帮助。

责任编辑:闫佳明 来源: oschina
相关推荐

2013-03-28 13:14:45

AIDL进程间通信Android使用AI

2023-11-06 08:22:34

AIDLAndroid通信

2014-07-17 11:36:27

Android Stu使用教程

2011-05-27 11:34:53

Android preference

2011-06-01 13:22:25

Android Alarm

2017-03-29 15:20:25

AndroidRootTools框架

2010-01-25 17:53:35

Android Lis

2016-10-20 08:46:17

2011-09-02 19:12:59

IOS应用Sqlite数据库

2009-05-30 09:29:52

AndroidGoogle移动OS

2023-12-04 07:18:05

Goswitch

2017-05-24 09:43:42

2013-06-08 17:09:35

Android开发移动开发XML解析

2013-02-19 13:42:30

Android开发string

2013-03-27 09:47:01

Android开发SQAndroid SDK

2021-06-30 07:19:36

Netty简单使用

2010-08-29 21:17:48

DHCP中继

2021-08-20 14:27:50

鸿蒙HarmonyOS应用

2011-06-02 10:37:02

Android 对话框

2018-07-02 13:10:05

Android短信验证
点赞
收藏

51CTO技术栈公众号