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. 2016/12/28_javascript

    今天学习的主要内容: javascript: 1.if语句,switch语句,while循环以及for循环: 1)if语句 if(boolean){}; if(boolean){} else if(b ...

  2. iOS中支付宝集成

    iOS中支付宝集成 如今各种的App中都使用了三方支付的功能,现在将我在使用支付宝支付集成过程的心得分享一下,希望对大家都能有所帮助 要集成一个支付宝支付过程的环境,大致需要: 1>公司:先与支 ...

  3. 使用apache自带日志分割模块rotatelogs,分割日志

    rotatelogs 是 Apache 2.2 中自带的管道日志程序,参数如下(参见:http://lamp.linux.gov.cn/Apache/ApacheMenu/programs/rotat ...

  4. Linux下高cpu解决方案

    昨天搞定了一个十万火急的issue,客户抱怨产品升级后系统会变慢和CPU使用率相当高,客户脾气很大,声称不尽快解决这个问题就退货,弄得我们 R&D压力很大,解决这个issue的任务分给了我,客 ...

  5. Xamarin.iOS开发初体验

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKwAAAA+CAIAAAA5/WfHAAAJrklEQVR4nO2c/VdTRxrH+wfdU84pW0

  6. .NET应用程序与数据库交互的若干问题

    我们知道,在应用程序中与数据库进行交互是一个比较耗时的过程,首先应用程序需要与应用程序建立连接,然后将请求发送到数据库,数据库执行操作,然后将结果集返回.所以在程序中,要尽量晚的与数据库建立连接,并且 ...

  7. [Intel Edison开发板] 04、Edison开发基于nodejs和redis的服务器搭建

    一.前言 intel-iot-examples-datastore 是Intel提供用于所有Edison开发板联网存储DEMO所需要的服务器工程.该工程是基于nodejs和redis写成的一个简单的工 ...

  8. python 三元运算

    C:\Users\Administrator>pythonPython 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.190 ...

  9. jQuery系列:选择器

    jQuery选择器通过标签名.属性名或内容对DOM元素进行选择,而不用担心浏览器的兼容性. 1. 基本选择器 基本选择器是jQuery中使用最频繁的选择器,由元素ID.class.元素名.多个选择符组 ...

  10. 文本比较算法:Needleman/Wunsch算法

    本文介绍基于最长公共子序列的文本比较算法——Needleman/Wunsch算法.还是以实例说明:字符串A=kitten,字符串B=sitting那他们的最长公共子序列为ittn(注:最长公共子序列不 ...