了解AIDL
1、什么是AIDL?
Android Interface Definition Lauguage(android接口描述语言)是一个IDL语言。
2、AIDL的作用?
背景:在android平台 中,一个进程通常不能访问其它进程中的内存区域。所以,他们需要把对象拆分成操作系统能理解的简单形式,以便伪装成对象跨越边界访问。编写这种伪装代码相当的枯燥乏味,好在android为我们提供了AIDL工具可以来做这件事
作用:用来进行进程间通信,有很多人可能就会问到,进程间通信有很多方法,为什么非要用AIDL了?进程间通信的确有很多,如广播、Message,Content Provider。这些是可以进行进程间通信,但是广播是单向,Message只能在同一个进程通信,而不能跨进程通信,Content Provider不是实时的。而AIDL拥有这他们所没有的优点,双向和跨进程通信、实时。
那在什么情况下使用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的更多相关文章
- Android探索之AIDL实现进程间通信
前言: 前面总结了程序间共享数据,可以使用ContentProvider也可以使用SharedPreference,那么进程间怎么共享内存呢?Android系统中的进程之间不能共享内存,因此,需要提供 ...
- Android开发aidl使用中linkToDeath和unlinkToDeath的使用
1.Binder死亡代理 这一节首先将介绍Binder类中比较重要的两个方法linkToDeath和unlinkToDeath.我们知道Binder是运行在服务进程,若服务端进程因为某种原因“ ...
- android不需要Socket的跨进程推送消息AIDL!
上篇介绍了跨进程实时通讯http://www.cnblogs.com/xiaoxiaing/p/5818161.html 但是他有个缺点就是服务端无法推送消息给客户端,今天这篇文章主要说的就是服务器推 ...
- Android中利用AIDL机制调用远程服务
服务端: //CalculateInterface.aidl package com.itheima.aidl.calculate; interface CalculateInterface { do ...
- Android中AIDL的理解与使用(二)——跨应用绑定Service并通信
跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...
- Android中AIDL的理解与使用(一)——跨应用启动/绑定Service
AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...
- 安卓中AIDL的使用方法快速入门
1.AIDL是什么? AIDL全称是Android Interface Definition Language,即安卓接口定义语言. 2.AIDL是用来做什么的?(为什么要有AIDL) AIDL是用来 ...
- 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: *** [ ...
- 使用AIDL调用远程服务设置系统时间
在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的.于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间 ...
随机推荐
- hbase协处理器编码实例
Observer协处理器通常在一个特定的事件(诸如Get或Put)之前或之后发生,相当于RDBMS中的触发器.Endpoint协处理器则类似于RDBMS中的存储过程,因为它可以让你在RegionSer ...
- Visual Studio Code 使用Git进行版本控制
Visual Studio Code 使用Git进行版本控制 本来认为此类教程,肯定是满网飞了.今天首次使用VS Code的Git功能,翻遍了 所有中文教程,竟没有一个靠谱的.遂动笔写一篇. 请确保你 ...
- 【初探Spring】------Spring IOC(二):初始化过程---简介
首先我们先来看看如下一段代码 ClassPathResource resource = new ClassPathResource("bean.xml"); DefaultList ...
- ASP.NET MVC Model元数据(四)
ASP.NET MVC Model元数据(四) 前言 前面的篇幅讲解了Model元数据生成的过程,并没有对Model元数据生成过程的内部和Model元数据结构的详细解释.看完本篇后将会对Model元数 ...
- stanford corenlp的TokensRegex
最近做一些音乐类.读物类的自然语言理解,就调研使用了下Stanford corenlp,记录下来. 功能 Stanford Corenlp是一套自然语言分析工具集包括: POS(part of spe ...
- [APUE]不用fcntl实现dup2函数功能
dup2的函数定义为: #include <unistd.h> int dup2(int src_fd, int new_fd); 自己实现dup2函数有几个关键点: 1,检查给定的源fd ...
- Java命名规范
驼峰法则: 将所有字母都小写(包括缩写),然后将单词的第一个字母大写. 每个单词的第一个字母都大写,来得到大驼峰式命名. 除了第一个单词,每个单词的第一个字母都大写,来得到(小)驼峰式命名. 为避免歧 ...
- 设计模式之迪米特原则(LOD)(最少知识原则)
来源:迪米特法则(LoD)最初是用来作为面向对象的系统设计风格的一种法则,是很多著名系统,如火星登陆软件系统.木星的欧罗巴卫星轨道飞船的软件系统的指导设计原则. 迪米特法则(LoD)又可分为两种:狭义 ...
- Git异常:fatal: could not create work tree dir 'XXX': No such file or directory
GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...
- Application Request Route实现IIS Server Farms集群负载详解
序言 随着公司业务的发展,后台业务就变的越来越多,然而服务器的故障又像月经一样,时不时的汹涌而至,让我们防不胜防.那么后台的高可用,以及服务器的处理能力就要做一个横向扩展的方案,以使后台业务持续的稳定 ...