安卓中AIDL的使用方法快速入门
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的使用方法快速入门的更多相关文章
- laravel 中CSS 预编译语言 Sass 快速入门教程
CSS 预编译语言概述 CSS 作为一门样式语言,语法简单,易于上手,但是由于不具备常规编程语言提供的变量.函数.继承等机制,因此很容易写出大量没有逻辑.难以复用和扩展的代码,在日常开发使用中,如果没 ...
- 安卓中onBackPressed ()方法的使用
一.onBackPressed()方法的解释 这个方法放在 void android.app.Activity.onBackPressed() 在安卓API中它是这样解释的: public void ...
- Python中的单元测试模块Unittest快速入门
前言 为什么需要单元测试? 如果没有单元测试,我们会遇到这种情况:已有的健康运行的代码在经过改动之后,我们无法得知改动之后是否引入了Bug.如果有单元测试的话,只要单元测试全部通过,我们就可以保证没有 ...
- Java中23种设计模式--超快速入门及举例代码
在网上看了一些设计模式的文章后,感觉还是印象不太深刻,决定好好记录记录. 原文地址:http://blog.csdn.net/doymm2008/article/details/13288067 注: ...
- Python中定时任务框架APScheduler的快速入门指南
前言 大家应该都知道在编程语言中,定时任务是常用的一种调度形式,在Python中也涌现了非常多的调度模块,本文将简要介绍APScheduler的基本使用方法. 一.APScheduler介绍 APSc ...
- webpack快速入门——实战技巧:watch的正确使用方法,webpack自动打包
随着项目大了,后端与前端联调,我们不需要每一次都去打包,这样特别麻烦,我们希望的场景是,每次按保存键,webpack自动为我们打包,这个工具就是watch! 因为watch是webpack自带的插件, ...
- webpack快速入门——CSS中的图片处理
1.首先在网上随便找一张图片,在src下新建images文件夹,将图片放在文件夹内 2.在index.html中写入代码:<div id="pic"></div& ...
- webpack快速入门——处理HTML中的图片
在webpack中是不喜欢你使用标签<img>来引入图片的,但是我们作前端的人特别热衷于这种写法, 国人也为此开发了一个:html-withimg-loader.他可以很好的处理我们在ht ...
- Redis快速入门:安装、配置和操作
本文是有关Redis的系列技术文章之一.在之前的文章中介绍了<Redis快速入门:初识Redis>,对Redis有了一个初步的了解.今天继续为大家介绍Redis如何安装.配置和操作. 系列 ...
随机推荐
- THINKPHP源码学习--------文件上传类
TP图片上传类的理解 在做自己项目上传图片的时候一直都有用到TP的上传图片类,所以要进入源码探索一下. 文件目录:./THinkPHP/Library/Think/Upload.class.php n ...
- jpa
学习尚硅谷jpa笔记: 所依赖的jar包: 首先在META-INF下创建配置文件,persistence.xml <?xml version="1.0" encoding=& ...
- 简单轮播js实现
<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8" ...
- docker学习(1) 安装
docker是啥就不多讲了,简言之就是更轻量.更牛叉的新一代虚拟机技术.下面是安装步骤: 一.mac/windows平台的安装 docker是在linux内核基础上发展而来的,无法直接运行在mac/w ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Word Frequency 单词频率
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- python读取excel一例-------从工资表逐行提取信息
在工作中经常要用到python操作excel,比如笔者公司中一个人事MM在发工资单的时候,需要从几百行的excel表中逐条的粘出信息,然后逐个的发送到员工的邮箱中.人事MM对此事不胜其烦,终于在某天请 ...
- NCspider项目总结
下午就要答辩了,想把项目经历再总结一下. 项目分三个阶段. 第一阶段,是信息搜集整理阶段 要想法设法从各个门户网站上抓取到新闻和对应的评论数据.首先要分析网站结构. 1. 从哪里找到网站每日发布的所有 ...
- 【Quartz】将定时任务持久化到数据库
之前的文章所做的demo是将定时任务的信息保存在内存中的,见以下配置 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore 如果,我们需要在 ...
- CSS你可能还不知道的一些知识点
一.特殊选择器 1.* 用于匹配任何的标记 2.> 用于指定父子节点关系 3.E + F 毗邻元素选择器,匹配所有紧随E元素之后的同级元素F 4.E ~ F 匹配所有E元素之后的同级元素F 5. ...