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. 【第四篇】ASP.NET MVC快速入门之完整示例(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  2. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  3. .net(C#)中this关键字

    使用this关键字引用成员变量使用this关键字在自身构造方法内部引用其它构造方法使用this关键字代表自身类的对象使用this关键字引用成员方法 在一个类的方法或构造方法内部,可以使用"t ...

  4. CocoaPods的那些坑

    CocoaPods的那些坑 文章转自http://blog.csdn.net/zhanniuniu/article/details/52159362#comments 我跟博主的经历超级像!不过自己用 ...

  5. Sql 2008 的常用函数

    1.LEN 函数:返回数据的长度 ') 返回:8 2.ASCII函数:返回字符串最左边的ascii值 SELECT ASCII('abc') 返回:97 3.LEFT函数:从左边开始截取指定长度的字符 ...

  6. 使用Mysql Workbench 画E-R图

    MySQL Workbench 是一款专为MySQL设计的ER/数据库建模工具.你可以用MySQL Workbench设计和创建新的数据库图示,建立数据库文档,以及进行复杂的MySQL 迁移.这里介绍 ...

  7. hibernate入门案例

    最近准备学ssh,今天学了一下hibernate,用的是hibernate4,现在已经出5了:配置文件很容易写错,写配置文件的时候尽量复制. 需要的jar包如下:(jar包我是直接放在项目工程里面了, ...

  8. 开启A20线(部分译)

    开启A20线 在查看或编写操作系统内核时一定会遇到A20线这个问题.本人对此一直都是似懂非懂的,查了些资料,决定弄明白于是有了这篇文章.其中前一部分是翻译一篇外国博文,但光有这篇文章依旧不能清楚地说明 ...

  9. pureftp在centos下与MySQL搭配使用

    概述 pure-ftpd是linux下的一个ftp服务端,据说安全性较高.我在centos6下用yum安装pure-ftpd,并配置了通过MySQL进行用户的增删改查,以及对应到apache的web目 ...

  10. 使用Navicat在oracle XE上新建表空间、用户及权限赋予

    参考资料: 烂泥:使用Navicat for Oracle新建表空间.用户及权限赋予 - 烂泥行天下 - 51CTO技术博客http://ilanni.blog.51cto.com/526870/12 ...