1、什么是AIDL?

  Android Interface Definition Lauguage(android接口描述语言)是一个IDL语言。

2、AIDL的作用?

    背景:在android平台 中,一个进程通常不能访问其它进程中的内存区域。所以,他们需要把对象拆分成操作系统能理解的简单形式,以便伪装成对象跨越边界访问。编写这种伪装代码相当的枯燥乏味,好在android为我们提供了AIDL工具可以来做这件事

    作用:用来进行进程间通信,有很多人可能就会问到,进程间通信有很多方法,为什么非要用AIDL了?进程间通信的确有很多,如广播、Message,Content Provider。这些是可以进行进程间通信,但是广播是单向,Message只能在同一个进程通信,而不能跨进程通信,Content Provider不是实时的。而AIDL拥有这他们所没有的优点,双向和跨进程通信、实时。

  那在什么情况下使用AIDL了?

  官方文档介绍:

    Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC(Inter-Process Communication,进程间通信)I 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
 
3、使用流程
  要使用AIDL,Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个stub服务桩类。Service的onBind方法会返回实现类的对象,之后你就可以使用它了
 
4、小程序应用
  首先创建一个后缀名为aidl的文件,我的为Manager.aidl

package com.test.service.aidl;

interface Manager{
    float add(float num1,float num2);
}

  写一个实现它的类ManagerImpl.java

package com.test.service.aidl;

public class ManagerImpl extends Manager.Stub{

    @Override
    public float add(float num1,float num2){
        return num1 + num2;
    }
}

  写一个服务类MyService.java

package com.test;

import com.test.service.aidl.Manager;
import com.test.service.aidl.ManagerImpl;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service{
    private Manager.Stub binder;

    public void onCreate(){
        super.onCreate();
        binder = new ManagerImpl();
        System.out.println("---->onCreate");
        Log.e("--->", "---->onCreate");
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("---->onStart");
        return super.onStartCommand(intent, flags, startId);
    }
    //外界要想访问服务,是通过返回的binder访问的
    @Override
    public IBinder onBind(Intent arg0) {
        return binder;
    }

}

  在AndroidMainfest.xml配置资源

  android:process=":push"是另外一个进程

<service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true"
            android:process=":push" >
        </service>

最后就写测试类了MainActivity.java

package com.test.ui.activity;

import com.test.service.aidl.Manager;
import com.test.MyService;
import com.test.R;
import com.test.service.aidl.ManagerImpl;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;

public class MainActivity extends Activity {
    private Manager binder;
    private EditText etNum1,etNum2;
    private TextView tvResult;

    private ServiceConnection serviceConnect = new XmppServiceConnect();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("---->onCreate1");
        init();
    }

    protected void onStart() {
        super.onStart();
     //活动开始时绑定MyService,就会自动调用serviceConnect bindService(new Intent(this, MyService.class), serviceConnect, BIND_AUTO_CREATE); } private void init() { etNum1 = (EditText)findViewById(R.id.etNum1); etNum2 = (EditText)findViewById(R.id.etNum2); tvResult = (TextView)findViewById(R.id.tvResult); } protected void onDestroy() { super.onDestroy(); unbindService(serviceConnect); } public void btn_add_click(View v) throws RemoteException{ float num2 = Float.parseFloat(etNum2.getText().toString()); float num1 = Float.parseFloat(etNum1.getText().toString()); tvResult.setText(binder.add(num1, num2)+""); } private class XmppServiceConnect implements ServiceConnection {
     //开始绑定服务时就执行下面的方法 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { binder = Manager.Stub.asInterface(iBinder);//特别注意这里iBinder是服务那边传过来的 }      //解绑时才调用 public void onServiceDisconnected(ComponentName componentName) { binder = null; } } }

界面就很简单了,两个EditText,一个TextView和一个Button

恩结束了,如果有错的地方,还希望有人指出来

了解AIDL的更多相关文章

  1. Android探索之AIDL实现进程间通信

    前言: 前面总结了程序间共享数据,可以使用ContentProvider也可以使用SharedPreference,那么进程间怎么共享内存呢?Android系统中的进程之间不能共享内存,因此,需要提供 ...

  2. Android开发aidl使用中linkToDeath和unlinkToDeath的使用

    1.Binder死亡代理     这一节首先将介绍Binder类中比较重要的两个方法linkToDeath和unlinkToDeath.我们知道Binder是运行在服务进程,若服务端进程因为某种原因“ ...

  3. android不需要Socket的跨进程推送消息AIDL!

    上篇介绍了跨进程实时通讯http://www.cnblogs.com/xiaoxiaing/p/5818161.html 但是他有个缺点就是服务端无法推送消息给客户端,今天这篇文章主要说的就是服务器推 ...

  4. Android中利用AIDL机制调用远程服务

    服务端: //CalculateInterface.aidl package com.itheima.aidl.calculate; interface CalculateInterface { do ...

  5. Android中AIDL的理解与使用(二)——跨应用绑定Service并通信

    跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...

  6. Android中AIDL的理解与使用(一)——跨应用启动/绑定Service

    AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...

  7. 安卓中AIDL的使用方法快速入门

    1.AIDL是什么? AIDL全称是Android Interface Definition Language,即安卓接口定义语言. 2.AIDL是用来做什么的?(为什么要有AIDL) AIDL是用来 ...

  8. make: *** [out/host/linux-x86/obj/EXECUTABLES/aidl_intermediates/aidl] 错误 1,make: *** [out/host/linux-x86/obj/lib/libESR_Portable.so] 错误 1

    错误3: g++: g++: selected multilib '32' not installed selected multilib '32' not installed make: *** [ ...

  9. 使用AIDL调用远程服务设置系统时间

    在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的.于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间 ...

随机推荐

  1. Android菜单项内容大全

    一.介绍: 菜单是许多应用中常见的用户界面组件. Android3.0版本以前,Android设备会提供一个专用"菜单"按钮呈现常用的一些用户操作, Android3.0版本以后, ...

  2. Linux实战教学笔记01:计算机硬件组成与基本原理

    标签(空格分隔): Linux实战教学笔记 第1章 如何学习Linux 要想学好任何一门学问,不仅要眼睛看,耳朵听,还要动手记,勤思考,多交流甚至尝试着去教会别人. 第2章 服务器 2.1 运维的基本 ...

  3. 客服小妹是如何泡到手的——C#定时提醒·语音录制·语音播放·文件转录Demo源码——倾情奉献!

    一.需求提出 客服小妹跟我说,每天要统计新加好友数,得先记下昨天的数目,然后查看今天的数目,还要相减,打字,记录——好麻烦! 又说,客户多的时候,忙起这头忘了那头,文字记录备忘又太费劲! 我说,赐你一 ...

  4. 在 Linux 中安装 Oracle JDK 8 以及 JVM 的类加载机制

    参考资料 该文中的内容来源于 Oracle 的官方文档 Java SE Tools Reference .Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以直接找 ...

  5. SQL Server 索引和表体系结构(非聚集索引)

    非聚集索引 概述 对于非聚集索引,涉及的信息要比聚集索引更多一些,由于整个篇幅比较大涉及接下来的要写的“包含列的索引”,“索引碎片”等一些知识点,可能要结合起来阅读理解起来要更容易一些.非聚集索引和聚 ...

  6. python的print函数的格式化输出

    使用print函数的时候,可以像C一样格式化输出,同时还支持参数化输出 print('%s' % ("CooMark")) print('整数|%d|' % (123)) prin ...

  7. 解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误

    一.问题描述 1.错误截图如下. 2.设计的问题 在Eclipse中新建了一个Maven工程, 然后更改JDK版本为1.6, 结果每次使用Maven > Update project的时候JDK ...

  8. .NET Core的文件系统[1]:读取并监控文件的变化

    ASP.NET Core 具有很多针对文件读取的应用.比如我们倾向于采用JSON文件来定义配置,所以应用就会涉及针对配置文件读取.如果用户发送一个针对物理文件的HTTP请求,应用会根据指定的路径读取目 ...

  9. Android Activity的4种启动模式详解(示例)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5233269.html 先介绍下Android对Activity的管理,Android采用Task来管理多个A ...

  10. 3.C#面向对象基础聊天机器人

    基于控制台的简单版的聊天机器人,词库可以自己添加. 聊天机器人1.0版本 源码如下: using System; using System.Collections.Generic; using Sys ...