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. 深入理解css3中nth-child和 nth-of-type的区别

    在css3中有两个新的选择器可以选择父元素下对应的子元素,一个是:nth-child 另一个是:nth-of-type. 但是它们到底有什么区别呢? 其实区别很简单::nth-of-type为什么要叫 ...

  2. 流程表单中js如何清空SheetUser控件数据?

    昨天有人问我js怎么清空.我试了试,发现简单的赋给他空值,并没有用.只能给他赋一个真实存在的值才有用.于是跟踪了一下他的删除按钮. 效果如下 使用场景:可以根据字段的不同类别变更人员. js代码如下, ...

  3. 解决使用IE8打开ADFS 3.0登录页面

    系统上线前一天,发现客户竟然有XP系统和2003系统,这些系统都不能访问外网.测试时,客户端是IE8,打开我们系统ADFS的登录页面,一直在Loading,无法打开,也不报错.后来通过fiddler跟 ...

  4. Android Studio快捷键

      一.android studio 默认快捷键 刚开始接触一款开发软件,想必很想了解它的快捷方式,这会对你的编程起到很好的帮助,提高工作效率,接下来给你介绍下Android Studio一些常用的快 ...

  5. Linux设备管理(二)_从cdev_add说起

    我在Linux字符设备驱动框架一文中已经简单的介绍了字符设备驱动的基本的编程框架,这里我们来探讨一下Linux内核(以4.8.5内核为例)是怎么管理字符设备的,即当我们获得了设备号,分配了cdev结构 ...

  6. 使用Hystrix提高系统可用性

    今天稍微复杂点的互联网应用,服务端基本都是分布式的,大量的服务支撑起整个系统,服务之间也难免有大量的依赖关系,依赖都是通过网络连接起来. (图片来源:https://github.com/Netfli ...

  7. CSS3变形记(上):千变万化的Div

    传统上,css就是用来对网页进行布局和渲染网页样式的.然而,css3的出现彻底打破了这一格局.了解过css3的人都知道,css3不但可以对网页进行布局和渲染样式,还可以绘制一些图形.对元素进行2D和3 ...

  8. 在春意盎然的季节里初识GIT

    Git 与 SVN 区别 GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等. 如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征. ...

  9. Android开发之自定义组件和接口回调

    说到自定义控件不得不提的就是接口回调,在Android开发中接口回调用的还是蛮多的.在这篇博客开始的时候呢,我想聊一下iOS的自定义控件.在iOS中自定义控件的思路是继承自UIView, 在UIVie ...

  10. C# 获取Excel中的合并单元格

    C# 获取Excel中的合并单元格 我们在制作表格时,有时经常需要合并及取消合并一些单元格.在取消合并单元格时需要逐个查找及取消,比较麻烦.这里分享一个简单的方法来识别Excel中的合并单元格,识别这 ...