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. java数组

    1.java是否可以像c一次样搞个不定长数组? 不可以那样写,那样写是非法的.数组构造的时候必须指定长度,因为JVM要知道需要在堆上分配多少空间.也就是要初始化数组的话让JVM知道要给数组分配多少空间 ...

  2. Entity Framework关于SQL注入安全问题

    1.EF生成的sql语句,用 parameter 进行传值,所以不会有sql注入问题 2.EF下有涉及外部输入参数传值的,禁止使用EF直接执行sql命令方式,使用实体 SQL   参考: https: ...

  3. codevs 1472 体检

    题目描述 Description 郑厂长不是正厂长 也不是副厂长 他根本就不是厂长 只是公司的一个码农 郑厂长所在的公司每一年都要组织员工体检,比如量身高体重.测血压之类的,今年也不例外. 这次总共有 ...

  4. [LeetCode] Rising Temperature 上升温度

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  5. modelsim实用教程--前言

    前言 Modelsim是一款专业的仿真软件,特别是在Quartus II 11.0之后的版本,都没有配套自身的仿真软件,所以Modelsim成了在FPGA设计流程中的进行功能仿真的首选仿真软件之一. ...

  6. C++11中的std::function

    看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::fun ...

  7. Python学习--Python基础语法

    第一个Python程序 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗 ...

  8. Oracle Entity6 在查询View的时候出现重复数据

    在使用oracle + Entity6的时候, 遇到一个问题,在查询View的时候,在工具中执行View的sql语句,是两条不同的数据. 但是从Entity中查询出来却是两条相同的数据. 查看了一下e ...

  9. oracle 修改字符集支持中文

    1.登录sys关闭服务 [oracle@t-e ~]$ export ORACLE_SID=kamiltest1 [oracle@t-e ~]$ sqlplus / as sysdba SQL> ...

  10. bzoj 1061 志愿者招募 有上下界费用流做法

    把每一天看作一个点,每一天的志愿者数目就是流量限制,从i到i+1连边,上下界就是(A[i],+inf). 对于每一类志愿者,从T[i]+1到S[i]连边,费用为招募一个志愿者的费用,流量为inf.这样 ...