AIDL:android interface define language(接口定义语言)

作用:方便远程调用其他服务中的方法

注意:安卓四大组件都要在清单文件注册

aidl创建图:

AIDL的全称是什么?如何工作?能处理哪些类型的数据?
AIDL全称Android Interface Definition Language(AndRoid接口描述语言)是一种接口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程跨界对象访问的目的.AIDL的IPC的机制和COM或CORBA类似, 是基于接口的,但它是轻量级的。它使用代理类在客户端和实现层间传递值.

如果要使用AIDL, 需要完成2件事情:

1. 引入AIDL的相关类.;

2. 调用aidl产生的class.

理论上, 参数可以传递基本数据类型和String, 还有就是Bundle的派生类, 不过在Eclipse中,目前的ADT不支持Bundle做为参数

具体实现步骤如下:
1、创建AIDL文件, 在这个文件里面定义接口, 该接口定义了可供客户端访问的方法和属性。
2、编译AIDL文件, 用Ant的话, 可能需要手动, 使用Eclipse plugin的话,可以根据adil文件自动生产java文件并编译, 不需要人为介入.
3、在Java文件中, 实现AIDL中定义的接口. 编译器会根据AIDL接口, 产生一个JAVA接口。这个接口有一个名为Stub的内部抽象类,它继承扩展了接口并实现了远程调用需要的几个方法。接下来就需要自己去实现自定义的几个接口了.
4、向客户端提供接口ITaskBinder, 如果写的是service,扩展该Service并重载onBind ()方法来返回一个实现上述接口的类的实例。
5、在服务器端回调客户端的函数. 前提是当客户端获取的IBinder接口的时候,要去注册回调函数, 只有这样, 服务器端才知道该调用那些函数
AIDL语法很简单,可以用来声明一个带一个或多个方法的接口,也可以传递参数和返回值。 由于远程调用的需要, 这些参数和返回值并不是任何类型.

下面是些AIDL支持的数据类型:
1. 不需要import声明的简单Java编程语言类型(int,boolean等)
2. String, CharSequence不需要特殊声明
3. List, Map和Parcelables类型, 这些类型内所包含的数据成员也只能是简单数据类型, String等其他比支持的类型.
(另外: 我没尝试Parcelables, 在Eclipse+ADT下编译不过, 或许以后会有所支持).

实现接口时有几个原则:
.抛出的异常不要返回给调用者. 跨进程抛异常处理是不可取的.
.IPC调用是同步的。如果你知道一个IPC服务需要超过几毫秒的时间才能完成地话,你应该避免在Activity的主线程中调用。也就是IPC调用会挂起应用程序导致界面失去响应. 这种情况应该考虑单起一个线程来处理.
.不能在AIDL接口中声明静态属性。

IPC的调用步骤:
1. 声明一个接口类型的变量,该接口类型在.aidl文件中定义。
2. 实现ServiceConnection。
3. 调用ApplicationContext.bindService(),并在ServiceConnection实现中进行传递.
4. 在ServiceConnection.onServiceConnected()实现中,你会接收一个IBinder实例(被调用的Service). 调用
YourInterfaceName.Stub.asInterface((IBinder)service)将参数转换为YourInterface类型。
5. 调用接口中定义的方法。你总要检测到DeadObjectException异常,该异常在连接断开时被抛出。它只会被远程方法抛出。
6. 断开连接,调用接口实例中的ApplicationContext.unbindService()
参考:http://buaadallas.blog.51cto.com/399160/372090

interface InterfaceAidl {
void interfacePay();
}
public class MyService extends Service {

    class MyBinder extends InterfaceAidl.Stub {
@Override
public void interfacePay() throws RemoteException {
pay();
}
} @Nullable
@Override
public IBinder onBind(Intent intent) {
MyBinder binder = new MyBinder();
return binder;
} private void pay() {
Log.i("service","支付成功");
}
}
public class MainActivity extends AppCompatActivity {
private Intent intent;
private MyServiceConnection myServiceConnection;
private InterfaceAidl interfaceAidl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this,MyService.class);
myServiceConnection = new MyServiceConnection();
} public void bind(View view){
bindService(intent, myServiceConnection, BIND_AUTO_CREATE);
} public void pay(View view){
try {
interfaceAidl.interfacePay();
} catch (RemoteException e) {
e.printStackTrace();
}
} class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
interfaceAidl = InterfaceAidl.Stub.asInterface(service);
} @Override
public void onServiceDisconnected(ComponentName name) { }
} @Override
protected void onDestroy() {
super.onDestroy();
unbindService(myServiceConnection);
}
}

远程调用服务

远程服务。
  1:将服务所在的项目中man目录下的aidl目录复制粘贴到当前项目。然后同步。会在项目的build-generated--source--
   aidl---debug--生成一个PayInterface.java文件.
  2:在MainActivity里创建意图。
    intent = new Intent();
          //从5.0开始,intent不能使用隐式意图
          intent.setAction("aidl_server.pay");
          intent.setPackage("com.qf.gao.d25_aidl_service");//指定包名,
  3:其他的跟调用本地服务一样。

interface InterfaceAidl {
void interfacePay();
}
<service android:name=".MyService">
<intent-filter>
<action android:name="aidl_interface.pay"></action>
</intent-filter>
</service>
public class MainActivity extends AppCompatActivity {

    private Intent intent;
private InterfaceAidl interfaceAidl;
private MyServiceConnection myServiceConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent();
myServiceConnection = new MyServiceConnection();
intent.setAction("aidl_interface.pay");
intent.setPackage("com.example.administrator.aidlservice");
} public void bind(View view){
bindService(intent,myServiceConnection,BIND_AUTO_CREATE);
} public void pay(View view){
try {
interfaceAidl.interfacePay();
} catch (RemoteException e) {
e.printStackTrace();
}
} class MyServiceConnection implements ServiceConnection{ @Override
public void onServiceConnected(ComponentName name, IBinder service) {
interfaceAidl = InterfaceAidl.Stub.asInterface(service);
} @Override
public void onServiceDisconnected(ComponentName name) { }
} @Override
protected void onDestroy() {
super.onDestroy();
unbindService(myServiceConnection);
}
}

使用AIDL远程调用服务中的方法的更多相关文章

  1. Android Service AIDL 远程调用服务 【简单音乐播放实例】

    Android Service是分为两种: 本地服务(Local Service): 同一个apk内被调用 远程服务(Remote Service):被另一个apk调用 远程服务需要借助AIDL来完成 ...

  2. Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务

    1. 概述 bindService() 绑定服务  可以得到服务的代理人对象,间接调用服务里面的方法. 绑定服务: 间接调用服务里面的方法.           如果调用者activity被销毁了, ...

  3. 架构设计:一种远程调用服务的设计构思(zookeeper的一种应用实践)

    在深入学习zookeeper我想先给大家介绍一个和zookeeper相关的应用实例,我把这个实例命名为远程调用服务.通过对这种应用实例的描述,我们会对zookeeper应用场景会有深入的了解. 远程调 ...

  4. 如何从零开始实现一个soa远程调用服务基础组件

    说起soa远程调用基础组件,最著名的莫过于淘宝的dubbo了,目前很多的大型互联网公司都有一套自己的远程服务调用分布式框架,或者是使用开源的(例如dubbo),或者是自己基于某种协议(例如hessia ...

  5. spring boot / cloud (八) 使用RestTemplate来构建远程调用服务

    spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...

  6. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  7. 使用RPC 调用NameNode中的方法

    用户在Client 端是很难对 NameNode中的信息进行直接访问的, 所以 ,在Hadoop系统中为 Client端 提供了一系列的方法调用,这些方法调用是通过RPC 方法来实现的, 根据RPC ...

  8. Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法

    1.绑定服务调用服务里面的方法,图解: 步骤: (1)在Activity代码里面绑定 bindService(),以bind的方式开启服务 :                     bindServ ...

  9. 在远程桌面服务中配置RD网关直接访问内网

    原文地址:http://wangchunhai.blog.51cto.com/225186/1139388/ 远程桌面网关(RD 网关)是一项角色服务,使授权远程用户可以从任何连接到 Internet ...

随机推荐

  1. 将List<int> 转换为用逗号连接为字符串

    List<, , , , }; string str = String.Join(",", testList.ConvertAll<string>(new Con ...

  2. 如何让ThinkPHP的模板引擎达到最佳效率

    默认情况下ThinkPHP框架系统默认使用的模板引擎是内置模板引擎.内置模板引擎支持模板文件中采用php原生态代码和模板标签的混合使用.ThinkPHP官方开发文档说,这种默认的内置模板引擎的性能是高 ...

  3. Song Jiang's rank list

     Song Jiang's rank list Time Limit:1000MS     Memory Limit:512000KB     64bit IO Format:%I64d & ...

  4. cocos基础教程(5)数据结构介绍之cocos2d::Vector

    cocos2d::Vector cocos2d::Vector<T>是一个封装好的能动态增长顺序访问的容器.cocos2d::Vector<T>中的元素是按序存取的,它的低层实 ...

  5. SpringMVC关于json、xml自动转换的原理研究

    SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/springMVC ...

  6. [Effective JavaScript 笔记]第36条:只将实例状态存储在实例对象中

    理解原型对象与其实例之间是一对多的关系,对于实现正确的对象行为很重要.常见的错误是不小心将每个实例的数据存储到了其原型中. 示例 一个实现了树型数据结构的类可能将子节点存储在数组中. 实例状态在原型中 ...

  7. Linux下更好用的帮助命令—cheat

    导读 Linux系统中,我们经常会用man命令来帮助查看这个命令的具体用法,man是很强大的,但是英语不好的同学用man用起来可能不那么顺手,自然而然的就出现了cheat命令,cheat命令就是通过简 ...

  8. UISerachBar / UISearchDisplayController

    1. UISerachBar 继承与UIView, 包含uitextfield, 并且实现了uitextfielddelegate代理的主要内容 含有取消按钮, 默认不显示 2. UISerachDi ...

  9. 发现Select等注入语句自动跳转Code

    CODE区域: <?php $str = $_GET["keyword"]; $str00 = strtolower($str); //strtolower 变为小写函数 $ ...

  10. CPU供电维修