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. JavaScript 正则表达式语法

    定义 JavaScript定义正则表达式有两种方法. 1.RegExp构造函数 var pattern = new RegExp("[bc]at","i"); ...

  2. Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决

    前提: 1.安装Android Studio(过程略) 2.官网下载OpenCV for Android 网址:http:opencv.org/downloads.html 我下载的是下图的版本 3. ...

  3. 2003-Can't connect to mysql server on localhost (10061)

    mysql数据库出现2003-Can't connect to mysql server on localhost (10061)问题 解决办法:查看wampserver服务器是否启动,如果没有启动启 ...

  4. [Django]用户权限学习系列之设计自有权限管理系统设计思路

    若在阅读本片文章遇到权限操作问题,请查看本系列的前两章! http://www.cnblogs.com/CQ-LQJ/p/5609690.html和http://www.cnblogs.com/CQ- ...

  5. 超千个节点OpenStack私有云案例(1):CERN 5000+ 计算节点私有云

    CERN:欧洲核子研究组织 本文根据以下几篇文章整理而来: https://www.openstack.org/summit/tokyo-2015/videos/presentation/unveil ...

  6. 分享MSSQL、MySql、Oracle的大数据批量导入方法及编程手法细节

    1:MSSQL SQL语法篇: BULK INSERT [ database_name . [ schema_name ] . | schema_name . ] [ table_name | vie ...

  7. embedding mono实战笔录(一)

    最近在给自己的服务器节点添加脚本功能,考虑到 执行性能.开发效率.调试效率.可维护性.严谨性 五大要素,最终选用C#作为脚本语言,并使用mono作为中间层,使其具备跨平台特性,以备具有在Windows ...

  8. 页面与ViewModel(上)

    在UWP淘宝与旺信中,笔者主要负责页面与控件的制作,这些工作看似简单,但要想做的全面细致仍然需要深入的思考.本文想分享一些在UWP旺信的制作过程中,笔者在UI页面与控件制作上体会到的一些心得.可能笔者 ...

  9. UML类图(下):关联、聚合、组合、依赖

    前言 上一篇文章UML类图(上):类.继承.实现,讲了UML类图中类.继承.实现三种关系及其在UML类图中的画法,本文将接着上文的内容,继续讲讲对象之间的其他几种关系,主要就是关联.聚合.组合.依赖, ...

  10. ucos实时操作系统学习笔记——任务间通信(消息)

    ucos另一种任务间通信的机制是消息(mbox),个人感觉是它是queue中只有一个信息的特殊情况,从代码中可以很清楚的看到,因为之前有关于queue的学习笔记,所以一并讲一下mbox.为什么有了qu ...