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. BPM始终服务于人,落脚于人

    数字经济时代下,云计算.大数据.移动互联已经成为当下企业必须采取的武装力量.随着互联网+.中国制造2025.工业4.0等国家战略的引导与支持,无数的企业在这场数字化浪潮中使尽浑身解数,想要抓住机遇奋力 ...

  2. 使用win10远程控制ubuntu16.04

    使用win10远程控制ubuntu16.04,网上很多需要安装xfce桌面的.今天介绍一下,不需要安装其他桌面,使用Ubuntu16.04自带桌面,漂亮美观. Ubuntu16.04端: 1.打开终端 ...

  3. Linux根文件系统分析之init和busybox

    Hi,大家好!我是CrazyCatJack.今天给大家讲解Linux根文件系统的init进程和busybox的配置及编译. 先简单介绍一下,作为一个嵌入式系统,要想在硬件上正常使用的话.它的软件组成大 ...

  4. Forward+ Rendering Framework

    近几天啃各种新技术时又一个蛋疼的副产品...额,算是把AMD的Forward+ Sample抄了一遍吧. 其实个人感觉这个AMD大肆宣传的Forward+跟Intel很早之前提的Tiled-Based ...

  5. js动态加载css和js

    之前写了一个工具类点此链接里面含有这段代码,感觉用处挺多,特意提出来 var loadUtil = { /* * 方法说明:[动态加载js文件css文件] * 使用方法:loadUtil.loadjs ...

  6. Atitit 智能云网络摄像机的前世今生与历史 优点  密码默认888888

    Atitit 智能云网络摄像机的前世今生与历史 优点  密码默认888888 用户名admin  密码aaaaaa 网络摄像机是一种结合传统摄像机与网络技术所产生的新一代摄像机,它可以将影像通过网络传 ...

  7. PDO

    'PDO'是数据访问抽象层'用mysqli类找到mysqli驱动根据驱动操作mysqli数据库'其他类找到sqlserver驱动根据驱动操作sqlserve数据库'PDO 访问其他数据库 PDO的用法 ...

  8. FireFox调试手机浏览器

    https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Firefox_for_Android IN THIS ARTICLE ...

  9. 用VB脚本批到导入字段到PowerDesigner

    在PowerDesigner使用脚本批量导入excel中记录的表结构信息,由于需要通过powerdesigner逆向工程创建一些sybase IQ的表,由于是接口数据,只有excel表,手动导入太耗时 ...

  10. java单例设计模式

    单例模式的特点: 1.单例类只能有一个对象(实例). 2.单例类必须自己创建自己的唯一实例 . 3.单例类必须给所有其他对象提供这一实例. 设置步骤: 1.将对象的应用成员变量用private来修饰. ...