(转自)可以参见:http://www.2cto.com/kf/201406/312244.html

1、为什么要有AIDL?

  无论学什么东西,最先得弄明白为什么要有这个东西,不要说存在即是合理,存在肯定合理,但是你还是没有明白。对于AIDL有一些人的浅显概念就是,AIDL可以跨进程访问其他应用程序,和其他应用程序通讯,那我告诉你,很多技术都可以访问,如广播(应用A在AndroidManifest.xml中注册指定Action的广播)应用B发送指定Action的广播,A就能收到信息,这样也能看成不同应用之间完成了通讯(但是这种通讯是单向的);还如ContentProvider,通过URI接口暴露数据给其他应用访问;但是这种都算不上是应用之间的通讯。可能最让人迷惑的是Android推出来了Messager,它就是完成应用之间的通讯的。那么为什么还要有AIDL呢,官方文档介绍AIDL中有这么一句话:

  Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.

  第一句最重要,“只有当你允许来自不同的客户端访问你的服务并且需要处理多线程问题时你才必须使用AIDL”,其他情况下你都可以选择其他方法,如使用Messager,也能跨进程通讯。可见AIDL是处理多线程、多客户端并发访问的。而Messager是单线程处理。还是官方文档说的明白,一句话就可以理解为什么要有AIDL。那么是不是这样的写个AIDL试试。

2、AIDL使用

  1. 定义AIDL文件

     // 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);
    }

    这段代码也是官方文档的。命名为IRemoteService.aidl,放在com.example.android包下(这个可以随意),保存后Android编译器会在gen目录下自动生成IRemoteService.java文件

  2. 定义我们的服务,DDService.java,并且需要在AndroidManifest.xml中注册,并添加“duanqing.test.aidl” 的ACTION。这样我们的服务端就完成了,把服务端运行到模拟器(或者手机上),等一会可以看一下打印信息,重点看“线程名”

     package com.example.service;
    
     import com.example.android.IRemoteService;
    
     import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.os.Process; public class DDService extends Service {
    @Override
    public void onCreate() {
    super.onCreate();
    System.out.println("DDService onCreate........" + "Thread: " + Thread.currentThread().getName());
    }
    @Override
    public IBinder onBind(Intent arg0) {
    System.out.println("DDService onBind");
    return mBinder;
    } private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
    public int getPid(){
    System.out.println("Thread: " + Thread.currentThread().getName());
    System.out.println("DDService getPid ");
    return Process.myPid();
    }
    public void basicTypes(int anInt, long aLong, boolean aBoolean,
    float aFloat, double aDouble, String aString) {
    System.out.println("Thread: " + Thread.currentThread().getName());
    System.out.println("basicTypes aDouble: " + aDouble +" anInt: " + anInt+" aBoolean " + aBoolean+" aString " + aString);
    }
    }; }
  3. 实现客户端测试代码 新建另一个工程,同样需要添加AIDL协议文件(这是一个标准的协议文件,定义对外服务),这里我列出来我的测试代码
     package com.example.aidlclient;
    
     import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.os.Process;
    import android.os.RemoteException;
    import android.view.View; import com.example.android.IRemoteService; public class MainActivity extends Activity {
    private IRemoteService remoteService;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    } ServiceConnection conn = new ServiceConnection() { @Override
    public void onServiceDisconnected(ComponentName name) {
    } @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    remoteService = IRemoteService.Stub.asInterface(service);
    try {
    int pid = remoteService.getPid();
    int currentPid = Process.myPid();
    System.out.println("currentPID: " + currentPid +" remotePID: " + pid);
    remoteService.basicTypes(12, 1223, true, 12.2f, 12.3, "我们的爱,我明白");
    } catch (RemoteException e) {
    e.printStackTrace();
    }
    System.out.println("bind success! " + remoteService.toString());
    }
    }; /**
    * 监听按钮点击
    * @param view
    */
    public void buttonClick(View view) {
    System.out.println("begin bindService");
    Intent intent = new Intent("duanqing.test.aidl");
    bindService(intent, conn, Context.BIND_AUTO_CREATE);
    } @Override
    protected void onDestroy() {
    super.onDestroy();
    unbindService(conn);
    }
    }
  4. 执行 点击客户端按钮,执行,看打印信息:
    看服务端打印,DDService onCreate..........Thread: main,主线程,当客户端调用服务端getPid方法时,服务端是在Thread: Binder2中执行,当客户端调用服务端basicType方法时,服务端是在Thread:Binder1中执行

 

4、

AIDL学习的更多相关文章

  1. android中的AIDL学习笔记

    一.定义 AIDL是用来解决进程间通信的(一般有四种方式:Activity.Service.ContentProvider.Broadcast Receiver),两个进程间无法直接通信,所以要用AI ...

  2. Android -- Service绑定解绑和aidl

    Service是安卓四大组件之一,先前讲到了Service的生命周期,以及非绑定类型的生命周期的例子,这次来分享一下绑定形式的. 应用组件(客户端)可以调用bindService()绑定到一个serv ...

  3. 如何自学Android--转

    原文地址:http://blog.csdn.net/lavor_zl/article/details/51217319 1. Java知识储备 本知识点不做重点讲解: 对于有基础的同学推荐看<J ...

  4. 如何自学 Android 的?

    http://android.jobbole.com/83380/ 1. Java知识储备 本知识点不做重点讲解:对于有基础的同学推荐看<Java编程思想>,巩固基础,查漏补全,了解并熟悉 ...

  5. 我是怎样自学 Android 的?

    1. Java知识储备 本知识点不做重点解说: 对于有基础的同学推荐看<Java编程思想>,巩固基础,查漏补全,了解并熟悉很多其它细节知识点. 对于没有基础的同学推荐看一本Java基础的书 ...

  6. Android 学习笔记 Service服务与远程通信...(AIDL)

    PS:这一章节看的我有几分迷茫,不是很容易理解...不过还好总算是明白了一大半了...基本的迷惑是解决了... 学习内容: 1.跨应用启动服务... 2.跨应用绑定服务... 3.跨应用实现通信... ...

  7. Android:学习AIDL,这一篇文章就够了(下)

    前言 上一篇博文介绍了关于AIDL是什么,为什么我们需要AIDL,AIDL的语法以及如何使用AIDL等方面的知识,这一篇博文将顺着上一篇的思路往下走,接着介绍关于AIDL的一些更加深入的知识.强烈建议 ...

  8. 【转】 Pro Android学习笔记(七八):服务(3):远程服务:AIDL文件

    目录(?)[-] 在AIDL中定义服务接口 根据AIDL文件自动生成接口代码 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.n ...

  9. (转载)Android:学习AIDL,这一篇文章就够了(下)

    前言 上一篇博文介绍了关于AIDL是什么,为什么我们需要AIDL,AIDL的语法以及如何使用AIDL等方面的知识,这一篇博文将顺着上一篇的思路往下走,接着介绍关于AIDL的一些更加深入的知识.强烈建议 ...

随机推荐

  1. [windows操作系统]windows管理

    1.磁盘管理: 1.1.使用DISKPART命令行工具创建扩展分区: windows自带有一个disk management(磁盘管理)工具,但在其中却找不到如何创建扩展分区(一般MBR分区格式需要扩 ...

  2. 带优先级的队列 - PHP实现

    很久以前写的一个功能,当时需要一个优先级的队列,特用新学的swoole写了一个简单的demo,仅满足当时的需求. 功能说明: 完全参考httpsqs增加优先级参数level 例:           ...

  3. 导出带图形的数据excel表

    public static string StatisticsSR(string parmStr) { try { StatisticsSRInfo parm = JsonConvert.Deseri ...

  4. 使用POSIX正则库匹配一行中多个结果

    正则匹配与正则表达式是什么东西我就不说了,在这里说下POSIX这个c语言正则库在对字符串进行正则匹配时取出多个结果的问题. 首先简单说明下POSIX正则库的几个函数和使用方法 第一个函数:int re ...

  5. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  6. colorPrimaryDark无法改变状态栏颜色

    设置完colorPrimaryDark后,这个颜色是改变状态栏的颜色的, colorPrimary是改变标题栏背景色的 发现状态栏一直是灰色. 然后在布局文件中 AndroidMainifest.xm ...

  7. HTTP Proxy Servlet 代理服务使用

    java servlet  代理服务器 1. 使用 maven  依赖 <dependency> <groupId>org.mitre.dsmiley.httpproxy< ...

  8. sqlserver 跨服务器访问数据

    需求:两个一模一样的表,分别分布在两个服务器的数据库上,现在要在一个表中,查看这两个表的内容,并让Id排序 1:在本地数据库查询分析器中,运行以下两段语句: --创建链接服务器 exec sp_add ...

  9. python百分比数比较大小

    python是无法识别百分比的,估计你的百分比是string,所以需要转成int # !/usr/bin/python3.4 # -*- coding: utf-8 -*- # 百分数转为int de ...

  10. oracle返回多个参数

    CREATE OR REPLACE PACKAGE BODY get_form_no_pub IS /*================================================ ...