Android中AIDL如何使用

发布时间:2021-06-28 15:42:42 作者:Leah
来源:亿速云 阅读:114

这期内容当中小编将会给大家带来有关Android中AIDL如何使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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

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

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

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

package com.android.server; public class CursorService extends ICursorInfo.Stub{     final boolean hasService;     public CursorService() {         hasService = initializeJNI();     }     public synchronized boolean start() {         if (hasService)             return start0();         return false;     }     public synchronized void stop() {         if (hasService)             stop0();     }     public synchronized void locate(int x, int y) {         if (hasService)             locate0(x, y);     }       public synchronized void move(int dx, int dy) {        if (hasService)            move0(dx, dy);     }       public synchronized void getLocation(int[] p) {         if (p == null)             throw new NullPointerException("p is null");         if (p.length < 2)             throw new IllegalArgumentException("p.len must >= 2");         if (hasService)             getPosition0(p);     }     public synchronized void setTimeout(int t) {         if (hasService)             setTimeout0(t);     }       public synchronized int getTimeout() {         if (hasService)             return getTimeout0();         return -1;     }       public void setBitmap(byte[] bmp, int width, int height) {         if(bmp == null)             throw new NullPointerException("bmp is null");         if(width < 0 || height < 0)            throw new IllegalArgumentException("width < 0 || height < 0");        if(width * height > bmp.length)             throw new IndexOutOfBoundsException("bmp less than width*height");         setBitmap0(bmp,width,height);     }

在其中实现你aAIDL中的方法

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

public class Manager {     private static final String TAG = "Manager";     private IInfo   iCurSer;     private Manager(){     }           public Manager(Context ctx){         this.context = ctx;         new Manager();     }              /**这里就可以与service正常通信,调用service中的方法**/     public void startService(){         Intent intent=new Intent("com.android.server.CursorService");         context.bindService(intent,new CursorServiceConnection(),                 Service.BIND_AUTO_CREATE);     }     /**      * 实现ServiceConnection接口      * */     public final class CursorServiceConnection implements ServiceConnection{        // 和CursorService绑定时系统回调这个方法         @Override         public void onServiceConnected(ComponentName name, IBinder service) {            // 此处不能使用强制转换, 应该调用Stub类的静态方法获得CursorService接口的实例对象            iCurSer=ICursorInfo.Stub.asInterface(service);         }           //解除和CursorService的绑定时系统回调这个方法         @Override         public void onServiceDisconnected(ComponentName name) {             iCurSer=null;         }     } }

上述就是小编为大家分享的Android中AIDL如何使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 谈谈Android Binder机制及AIDL使用
  2. Android进阶之AIDL的使用详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

aidl android

上一篇:SQL中自连接的示例分析

下一篇:JavaScript中延迟加载属性的原理和用法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》