• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > Android中AIDL详解

Android中AIDL详解

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了android aidl详解,android中aidl,android studio aidl,android aidl,android service aidl等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Android中AIDL详解


介绍

Android Interface Definition Language (AIDL), Android接口定义语言。系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信Interprocess communication (IPC)。AIDL就是解决这个问题的。
阅读本文需要了解Service的相关知识

创建.aidl文件

aidl是用Java语法编写的,后缀为.aidl的文件。

每一个aidl文件必须定义一个接口,在这个接口里声明方法 在aidl里不能有static属性(field) aidl支持基本的数据类型,当你需要使用额外的数据类型时需要把它们import进来,即使它们跟这个文件在同一个包中。

示例

// IRemoteService.aidl
package com.example.android;

// Declare any non-default types here with import statements

/** Example service interface */
interface IRemoteService {
    /** Request the process ID of this service, to do evil things with it. */
    int getPid();

    /** Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

把aidl文件存在src/目录下,当你build项目的时候,SDK工具会在gen/目录下生成一个与.aidl文件名字相同的.java文件。

实现接口

生成的IRemoteService.java如下

public interface IRemoteService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.spark.meizi.IRemoteService
{
private static final java.lang.String DESCRIPTOR = "com.spark.meizi.IRemoteService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.spark.meizi.IRemoteService interface,
 * generating a proxy if needed.
 */
public static com.spark.meizi.IRemoteService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.spark.meizi.IRemoteService))) {
return ((com.spark.meizi.IRemoteService)iin);
}
return new com.spark.meizi.IRemoteService.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_basicTypes:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
long _arg1;
_arg1 = data.readLong();
boolean _arg2;
_arg2 = (0!=data.readInt());
float _arg3;
_arg3 = data.readFloat();
double _arg4;
_arg4 = data.readDouble();
java.lang.String _arg5;
_arg5 = data.readString();
this.basicTypes(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.spark.meizi.IRemoteService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
/**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
@Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(anInt);
_data.writeLong(aLong);
_data.writeInt(((aBoolean)?(1):(0)));
_data.writeFloat(aFloat);
_data.writeDouble(aDouble);
_data.writeString(aString);
mRemote.transact(Stub.TRANSACTION_basicTypes, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
/**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException;
}

在这个文件中有一个内部类Stub,这是父接口的一个抽象实现,并声明了aidl中的所有方法。为了实现由aidl生成的接口,我们需要继承Stub并实现从aidl继承过来的方法。下面是一个使用匿名类的例子

private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
    public int getPid(){
        return Process.myPid();
    }
    public void basicTypes(int anInt, long aLong, boolean aBoolean,
        float aFloat, double aDouble, String aString) {
        // Does nothing
    }
};

这样mBinder就是一个Stub的实例了,下一步就是如何在client端使用,与service端产生交互了。
注:

不能保证请求是在主线程被执行的,所以从构建到使用要考虑Service线程的安全性 默认情况下,请求是同步的,所以尽量不要在主线程中发出请求 所有的异常都不会返回给请求者(Caller)

在Client中使用接口

当你已经实现你的Service之后,你需把它暴露在Client中使Client可以绑定它。继承Service并实现onBind()方法,来返回一个实现了Stub的实例。下面就是一个把IRemoteService暴露给Client的例子:

public class RemoteService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Return the interface
        return mBinder;
    }

    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
        public int getPid(){
            return Process.myPid();
        }
        public void basicTypes(int anInt, long aLong, boolean aBoolean,
            float aFloat, double aDouble, String aString) {
            // Does nothing
        }
    };
}

现在client(比如是一个Activity)可以调用bindService()来连接这个Service,通过onServiceConnected()来接收Service中 onBind() 返回的mBinder,最后使用YourServiceInterface.Stub.asInterface(service)来把返回的mBinder转换成YourServiceInterface类型。例子如下:

IRemoteService mIRemoteService;
private ServiceConnection mConnection = new ServiceConnection() {
    // Called when the connection with the service is established
    public void onServiceConnected(ComponentName className, IBinder service) {
        // Following the example above for an AIDL interface,
        // this gets an instance of the IRemoteInterface, which we can use to call on the service
        mIRemoteService = IRemoteService.Stub.asInterface(service);
    }

    // Called when the connection with the service disconnects unexpectedly
    public void onServiceDisconnected(ComponentName className) {
        Log.e(TAG, "Service has unexpectedly disconnected");
        mIRemoteService = null;
    }
};

注:
如果Service和Client在两个不同的Application中,Client的Application的src/目录下必须也有对应的.aidl文件。

一个Client的例子

public static class Binding extends Activity {
    /** The primary interface we will be calling on the service. */
    IRemoteService mService = null;
    /** Another interface we use on the service. */
    ISecondary mSecondaryService = null;

    Button mKillButton;
    TextView mCallbackText;

    private boolean mIsBound;

    /**
     * Standard initialization of this activity.  Set up the UI, then wait
     * for the user to poke it before doing anything.
     */
    



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 使用AIDL调用远程服务设置系统时间,aidl调用系统
  • android 浅谈Aidl 通讯机制,androidaidl
  • 【Android】由浅到深理解AIDL
  • Android IPC 之 AIDL(一)
  • android使用AIDL实现跨进程通讯(IPC)
  • Android中AIDL详解

相关文章

  • 2017-05-228.3.5 Paint API之—— Xfermode与PorterDuff详解(二)
  • 2017-05-26Android便携式热点的开启状态检测和SSID的获取,android热点ssid
  • 2017-05-26做了5年软件测试了,写写心得
  • 2017-05-26Android Studio 2.1 Preview有那些更新内容
  • 2017-05-26npm源切换,npm源
  • 2017-05-26andriod arcgis加载影像TIF,andriodarcgis
  • 2017-05-26ORB_SLAM2在Android上的移植过程
  • 2017-05-26新闻客户端应用项目源码,客户端项目源码
  • 2017-05-26Android 贝塞尔曲线实现QQ拖拽清除效果
  • 2017-05-26硅谷新闻5--顶部新闻轮播图事件处理,硅谷5--

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • GreenDao与ReactiveX的完美搭配,greendaoreactivex
    • Android开发技巧——大图裁剪
    • Android之获取数据库路径,android获取数据库
    • Atitit.反编译apk android源码以及防止反编译apk,atititapk
    • Android系统 应用图标显示未读消息数(BadgeNumber) 桌面app图标的角标显示
    • 硅谷社交12--群列表页面,硅谷社交12--列表
    • 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新,androidtinker
    • 2.5.8 Notification(状态栏通知)详解
    • Android studio 百度地图开发(2)地图定位
    • android Listview的自定义界面的使用

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有