zoukankan      html  css  js  c++  java
  • 【转】 Pro Android学习笔记(七八):服务(3):远程服务:AIDL文件

    目录(?)[-]

    1. 在AIDL中定义服务接口
    2. 根据AIDL文件自动生成接口代码

    文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处:http://blog.csdn.net/flowingflying/

    Remote Service在之前的学习笔记 Android学习笔记(五三):服务Service(下)- Remote Service中介绍过。远程服允许行其他应用调用,及允许RPC(remote procedure call)。在Android中remote需要通过ADIL(Android interface definition Language)定义开放给client的接口。建立远程服务需要一下步骤:

    1、编写AIDL文件来定义向client开放的接口,并将AIDL文件加到项目src/目录下。

    2、实现service,在onBind()中返回接口

    3、在AndroidManifest.xml中配置service。

    在AIDL中定义服务接口

    我们将通过一个简单小例子也学习。例子很简单,远程服务模拟股指报价,给出请求,回复股价。我们将是src/目录下,即在我们增加java code的地方,加入一个IStockQuoteSerice.aidl文件,AIDL文件采用java语法,文件中的包名就是项目的包名。

    package cn.wei.flowingflying.proandroidservice

    interface IStockQuoteService
        double getQuote(String ticker); 
    }

    根据AIDL文件自动生成接口代码

    在编译之后,系统会根据aidl文件生成相应的IStockQuoteService.java文件,将接口以java的方式表达出来。Android对于一些复杂的接口,为了开发者使用便捷,自动生成相应的java文件。

    我们来看看自动生成的接口文件IStockQuoteService.java。自动生成代码的缩退没有那么好看,利用eclipse的聚合功能,IStockQuoteService中含有两部分,一个是class stub,另一个是我们在aidl中定义的接口函数getQuote()。

    /* 根据我们在aidl文件中对接口interface IStockQuoteService{}的定义,自动生成接口代码IStockQuoteService
    * This file is auto-generated.  DO NOT MODIFY. 
    * Original file: ...\src\cn\wei\flowingflying\proandroidservice\IStockQuoteService.aidl
    */ 
    package cn.wei.flowingflying.proandroidservice;/*这就是为何aidl中的package名字要和project的一致*/
    public interface IStockQuoteService extends android.os.IInterface


    /** 有一个抽象类Stub集成Binder,而同时实现IStockQuoteService。Stub在远程服务中很关键,当client连接server,即bindService()时,server返回一个Stub对象,由于stub实现接口,client可以通过这个对象来访问接口.Local-side IPC implementation stub class. */
    public static abstract class Stub extends android.os.Binder implements cn.wei.flowingflying.proandroidservice.IStockQuoteService

    private static final java.lang.String DESCRIPTOR = "cn.wei.flowingflying.proandroidservice.IStockQuoteService";
    /** Construct the stub at attach it to the interface. */ 
    public Stub() { ... ...} 
    /** Cast an IBinder object into an cn.wei.flowingflying.proandroidservice.IStockQuoteService interface, generating a proxy if needed. */
    public static cn.wei.flowingflying.proandroidservice.IStockQuoteService asInterface(android.os.IBinder obj) { … … }
    @Override public android.os.IBinder asBinder() { … … } 
    @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { … … }
    /* Proxy是Stub的内部类,实现IStockQuoteService接口,对接口中的getQuote()做了具体的实现*/ 
    private static class Proxy implements cn.wei.flowingflying.proandroidservice.IStockQuoteService


    … … 
    /*这是在class stub中对getQuote()的实现,具体的代码没看懂,可以理解为service和stub之间对getQuote()的结果的传递,可得到service的getQuote()的结果*/
    @Override public double getQuote(java.lang.String ticker) throws android.os.RemoteException
    {
     
    android.os.Parcel _data = android.os.Parcel.obtain(); 
    android.os.Parcel _reply = android.os.Parcel.obtain(); 
    double _result; 
    try { 
    _data.writeInterfaceToken(DESCRIPTOR); 
    _data.writeString(ticker); 
    mRemote.transact(Stub.TRANSACTION_getQuote, _data, _reply, 0); 
    _reply.readException(); 
    _result = _reply.readDouble(); 

    finally { 
    _reply.recycle(); 
    _data.recycle(); 

    return _result; 

    } 
    static final int TRANSACTION_getQuote = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);

    public double getQuote(java.lang.String ticker) throws android.os.RemoteException;

    本笔记涉及的例子代码,可以在Pro Android学习:Android service小例子中下载。

    相关链接: 我的Android开发相关文章

  • 相关阅读:
    非关系数据库之mongodb入门到实战(8)mongodb的常用命令
    非关系数据库之mongodb入门到实战(7)mongodb的window版安装与使用详解
    非关系数据库之mongodb入门到实战(6)mongodb基础详解
    非关系数据库之redis入门到实战(5)Redis企业运用
    非关系数据库之redis入门到实战(4)Redis基本操作
    非关系数据库之redis入门到实战(3)Redis入门介绍与安装
    非关系数据库之redis入门到实战(2)Redis常用命令
    非关系数据库之redis入门到实战(1)Redis高级应用
    Java从入门到实战之(16)面向对象之对象的多态性(四)
    Java从入门到实战之(15)面向对象之接口(三)
  • 原文地址:https://www.cnblogs.com/blongfree/p/5048065.html
Copyright © 2011-2022 走看看