1.AIDL是什么?

AIDL全称是Android Interface Definition Language,即安卓接口定义语言。

2.AIDL是用来做什么的?(为什么要有AIDL)

AIDL是用来进行进程间通信(IPC全称interprocess communication )的。

3.如何使用AIDL?

对于AIDL的使用,

服务端需要完成的任务是:

①.写一个xxxx.aidl文件

②.写一个Service并在AndroidManifest.xml中声明它。(注意:这个service里面有一个引用了实现xxxx.Stub抽象类的IBinder对象,这个对象将在service的onBind方法里面返回给调用者)

客户端的任务:

①.使用和服务端相同的那个aidl文件

②.在实现了ServiceConnection接口的onServiceConnected(ComponentName name, IBinder service)方法中调用myvar = testidl.Stub.asInterface(service)保存得到的对象,其中myvar是xxxx的类型

这么说还是不够清楚,下面直接上代码。

首先是服务端的

//testidl.idl文件的内容
package com.example.xxnote; interface testidl {
void TestFunction(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}

 

//myaidlservice.java文件的内容
package com.example.xxnote; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException; public class myaidlservice extends Service { private final IBinder myStub = new testidl.Stub() { @Override
public void TestFunction(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString)
throws RemoteException {
// TODO Auto-generated method stub
System.out.println("basicTypes()");
System.err.println("Service"+anInt + "," + aLong + "," + aBoolean + ","
+ aFloat + "," + aDouble + "," + aString);
}
}; @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("AIDL Service onBind, and return IBinder");
return myStub;
} @Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("AIDL Service onCreate");
super.onCreate();
} @Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("AIDL Service onUnbind");
return super.onUnbind(intent);
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("AIDL Service onDestroy");
super.onDestroy();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("AIDL Service onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
}

 

 <!--  AndroidManifest.xml 的 application 标签的内容-->
<service android:name="myaidlservice">
<intent-filter >
<action android:name="zhenshi.mafan.qisia.aidl"/>
</intent-filter>
</service>

对于客户端,首先需要把aidl文件复制到相应的目录本例中是src/com/example/xxnote/testidl.aidl

package com.example.xxnote.callaidl;

import com.example.xxnote.testidl;

import android.R.bool;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity { private testidl mytTestidl;
private ServiceConnection connection;
private boolean isServiceConnected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connection = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("Client onServiceDisconnected");
} @Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("Client onServiceConnected");
mytTestidl = testidl.Stub.asInterface(service);
isServiceConnected = true;
}
};
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
    
//关闭服务按钮的事件
public void StopMyService(View v) {
System.out.println("StopMyService");
isServiceConnected = false;
unbindService(connection);
mytTestidl = null;
}
     //开启服务按钮的事件
public void StartMyService(View v) {
System.out.println("before bindService()");
bindService(
new Intent().setAction("zhenshi.mafan.qisia.aidl"),
connection,
Context.BIND_AUTO_CREATE);
/**
* bindService是异步的所以执行bindService方法的同时也开始执行下面的方法了,
* Debug跟踪了一下程序发现貌似Activity里面所有的方法都是在主线程的loop()方法
* 循环里面以消息队列里面的一个消息的样子执行的,也就是此处的StartMyService方
* 法对应的消息处理完(此函数返回)后,才能处理下一个消息,即执行onServiceConnected回调方法
*
* 试验了一下,StopMyService里面把mytTestidl赋值为null,即每次解除服务绑定后都重置mytTestidl为null
* 果然每次下面的语句:
* mytTestidl.basicTypes(1, 1, true, 100.0f, 200.0, "ssss");
* 都报空指针异常
*/ try {
mytTestidl.TestFunction(1, 1, true, 100.0f, 200.0, "ssss");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

  

这里还有一个需要注意的地方,就是bindService方法 的时候用到的intent,通过setAction可以成功启动服务,用setClassName就不能,不知道什么原因,暂时留待以后解决。

找到原因了,用setClassName的时候必须使用全限定类名,如:new Intent().setClassName("com.example.client.callaidl", "com.example.client.callaidl.testact")

 

安卓中AIDL的使用方法快速入门的更多相关文章

  1. laravel 中CSS 预编译语言 Sass 快速入门教程

    CSS 预编译语言概述 CSS 作为一门样式语言,语法简单,易于上手,但是由于不具备常规编程语言提供的变量.函数.继承等机制,因此很容易写出大量没有逻辑.难以复用和扩展的代码,在日常开发使用中,如果没 ...

  2. 安卓中onBackPressed ()方法的使用

    一.onBackPressed()方法的解释 这个方法放在 void android.app.Activity.onBackPressed() 在安卓API中它是这样解释的: public void ...

  3. Python中的单元测试模块Unittest快速入门

    前言 为什么需要单元测试? 如果没有单元测试,我们会遇到这种情况:已有的健康运行的代码在经过改动之后,我们无法得知改动之后是否引入了Bug.如果有单元测试的话,只要单元测试全部通过,我们就可以保证没有 ...

  4. Java中23种设计模式--超快速入门及举例代码

    在网上看了一些设计模式的文章后,感觉还是印象不太深刻,决定好好记录记录. 原文地址:http://blog.csdn.net/doymm2008/article/details/13288067 注: ...

  5. Python中定时任务框架APScheduler的快速入门指南

    前言 大家应该都知道在编程语言中,定时任务是常用的一种调度形式,在Python中也涌现了非常多的调度模块,本文将简要介绍APScheduler的基本使用方法. 一.APScheduler介绍 APSc ...

  6. webpack快速入门——实战技巧:watch的正确使用方法,webpack自动打包

    随着项目大了,后端与前端联调,我们不需要每一次都去打包,这样特别麻烦,我们希望的场景是,每次按保存键,webpack自动为我们打包,这个工具就是watch! 因为watch是webpack自带的插件, ...

  7. webpack快速入门——CSS中的图片处理

    1.首先在网上随便找一张图片,在src下新建images文件夹,将图片放在文件夹内 2.在index.html中写入代码:<div id="pic"></div& ...

  8. webpack快速入门——处理HTML中的图片

    在webpack中是不喜欢你使用标签<img>来引入图片的,但是我们作前端的人特别热衷于这种写法, 国人也为此开发了一个:html-withimg-loader.他可以很好的处理我们在ht ...

  9. Redis快速入门:安装、配置和操作

    本文是有关Redis的系列技术文章之一.在之前的文章中介绍了<Redis快速入门:初识Redis>,对Redis有了一个初步的了解.今天继续为大家介绍Redis如何安装.配置和操作. 系列 ...

随机推荐

  1. LAMP环境配置 linux+apache+mysql+php

    虚拟机安装Linux系统: 新建虚拟机过程中选择Linux,下面选择centos或者是Ubuntu Linux切换图像命令:注意只有装了图像界面才可以切换 查看安装环境的版本: rpm -qa 查看安 ...

  2. 递推 hdu 1330

    http://www.cnblogs.com/rainydays/archive/2013/01/16/2862235.html 看样例的答案 #include<stdio.h> #inc ...

  3. EF 二级缓存 EFSecondLevelCache

    EFSecondLevelCache ======= Entity Framework .x Second Level Caching Library. 二级缓存是一个查询缓存.EF命令的结果将存储在 ...

  4. css-css权威指南学习笔记6

    第八章 padding/border/margin 1.对于只包含文本的行,能改变行间距里的属性只有line-height/font-size/vertical-align. 2.对行内非替换元素应用 ...

  5. [POJ&HDU]杂题记录

    POJ2152 树形dp,每次先dfs一遍求出距离再枚举所有点转移即可. #include<iostream> #include<cstdio> #include<cma ...

  6. map

    说明 map()是python的内置函数. 定义:接收2个参数,第一个参数一般为方法:第二个参数为可迭代对象,此方法会自动迭代第二个参数,然后将获取的数据传入第一个参数. 案例操作 需求:将下面的数据 ...

  7. SpringBoot IntelliJ创建简单的Restful接口

    使用SpringBoot快速建服务,和NodeJS使用express几乎一模一样,主要分为以下: 1.添加和安装依赖  2.添加路由(即接口) 3.对路由事件进行处理 同样坑的地方就是,祖国的防火墙太 ...

  8. UVA725

    虽然是暴力求解,但是也要观察条件,尽量提高效率.如本题,原本要枚举10个数,但是分析可知通过枚举fghij就可以了. #include<stdio.h> #include<strin ...

  9. STL的使用

    Vector:不定长数组 Vector是C++里的不定长数组,相比传统数组vector主要更灵活,便于节省空间,邻接表的实现等.而且它在STL中时间效率也很高效:几乎与数组不相上下. #include ...

  10. 浅谈:javascript的面向对象编程之基础知识的介绍

    在进入javascript的面对对象之前,我们先来介绍一下javascript的几个概念. 1.javascript的面向对象的基本概念 function aa(){ } /* * 这里的aa,在我们 ...