android AIDL 语言用法
跨进程通信可以用AIDL语言
这里讲述下如何使用AIDL语言进行跨进程通信
文章参考 《设计模式》一书
demo结构参考

主要的文件类有:IBankAidl.aidl
java文件:AidlBankBinder,BackActivity(应该是BankActivity写错了),BankService(继承自Service,服务类)
IBankAidl.aidl文件 这里AIdl的使用对包位置有要求,所以我就把包名放出来了
package finishdemo.arcturis.binderandserviceandaidl.binder;
// Declare any non-default types here with import statements
interface IBankAidl {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
/**
* 开户
* @param name 户主名
* @param password 密码
* @return 开户信息
*/
String openAccount(String name,String password);
/**
* 存钱
* @param money
* @param account
* @return
*/
String saveMoney(int money,String account);
/**
* 取钱
* @param money
* @param account
* @param password
* @return
*/
String takeMoney(int money,String account,String password);
/**
* 销户
* @param account
* @param password
* @return
*/
String closeAccount(String account,String password);
}
AidlBankBinder文件 继承自IBankAidl的Stub类,然后重写并实现Aidl内的方法,这里是模拟一个银行的操作
public class AidlBankBinder extends IBankAidl.Stub {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String openAccount(String name, String password) {
return name+"开户成功!账号为:"+ UUID.randomUUID().toString();
}
@Override
public String saveMoney(int money, String account) {
return "账户:"+account + "存入"+ money + "单位:人民币";
}
@Override
public String takeMoney(int money, String account, String password) {
return "账户:"+account + "支取"+ money + "单位:人民币";
}
@Override
public String closeAccount(String account, String password) {
return account + "销户成功";
}
}
BankService文件 返回一个AidlBankBinder
public class BankService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
//单进程写法
// return new BankBinder();
//不同进程AIDL 通信写法
return new AidlBankBinder();
}
}
接下来在Activity中使用 BackActivity方法
public class BackActivity extends AppCompatActivity implements View.OnClickListener {
// private BankBinder mBankBinder;
private IBankAidl mBankBinder;
private TextView tvMsg;
private Context context;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//同一进程写法
// mBankBinder = (BankBinder) service;
//不同进程写法
mBankBinder = IBankAidl.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
tvMsg = (TextView) findViewById(R.id.tv_msg);
Intent intent = new Intent();
intent.setAction("actionname.aidl.bank.BankService");
//这里遇到一个问题,如果你直接使用intent 这个意图去开启服务的话就会报
//Android Service Intent must be explicit 意思是服务必须要显示的调用,这个是5.0之后新的规定
//这个 createExplicitFromImplicitIntent 可以将隐性调用变成显性调用
Intent intent1 = new Intent(createExplicitFromImplicitIntent(context,intent));
bindService(intent1,conn,BIND_AUTO_CREATE);
initBtn(R.id.btn_aidl_bank_close);
initBtn(R.id.btn_aidl_bank_open);
initBtn(R.id.btn_aidl_bank_save);
initBtn(R.id.btn_aidl_bank_take);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
private void initBtn(int resID){
Button b = (Button) findViewById(resID);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_aidl_bank_open:
//这个RemoteException 是 AIDL跨进程通信的用法必须加上的异常捕获
try {
tvMsg.setText(mBankBinder.openAccount("BigAss","123456"));
}catch (RemoteException e){
e.printStackTrace();
}
break;
case R.id.btn_aidl_bank_save:
try {
tvMsg.setText(mBankBinder.saveMoney(8888888,"bigAss123"));
}catch (RemoteException e){
e.printStackTrace();
}
break;
case R.id.btn_aidl_bank_take:
try {
tvMsg.setText(mBankBinder.takeMoney(520,"bigAss123","123456"));
}catch (RemoteException e){
e.printStackTrace();
}
break;
case R.id.btn_aidl_bank_close:
try {
tvMsg.setText(mBankBinder.closeAccount("bigAss123","123456"));
}catch (RemoteException e){
e.printStackTrace();
}
break;
}
}
/***
* Android L (lollipop, API 21) introduced a new problem when trying to invoke implicit intent,
* "java.lang.IllegalArgumentException: Service Intent must be explicit"
*
* If you are using an implicit intent, and know only 1 target would answer this intent,
* This method will help you turn the implicit intent into the explicit form.
*
* Inspired from SO answer: http://stackoverflow.com/a/26318757/1446466
* @param context
* @param implicitIntent - The original implicit intent
* @return Explicit Intent created from the implicit original intent
*/
public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
}
主要思路是用隐式的方法启动一个服务,然后调用Aidl实例类的方法即可,具体的Aidl语言在BuildApp之后在

这里即可查看 内部是他实现进程间数据传输所做的转化代码,有兴趣可以看下
android AIDL 语言用法的更多相关文章
- Android AIDL的用法
一.什么是AIDL服务 一般创建的服务并不能被其他的应用程序访问.为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Cal ...
- Android webservice的用法详细讲解
Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...
- Android aidl Binder框架浅析
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38461079 ,本文出自[张鸿洋的博客] 1.概述 Binder能干什么?B ...
- AIDL/IPC Android AIDL/IPC 进程通信机制——超具体解说及使用方法案例剖析(播放器)
首先引申下AIDL.什么是AIDL呢?IPC? ------ Designing a Remote Interface Using AIDL 通常情况下,我们在同一进程内会使用Binder.Broad ...
- (转载)你真的理解Android AIDL中的in,out,inout么?
前言 这其实是一个很小的知识点,大部分人在使用AIDL的过程中也基本没有因为这个出现过错误,正因为它小,所以在大部分的网上关于AIDL的文章中,它都被忽视了——或者并没有,但所占篇幅甚小,且基本上都是 ...
- Android AIDL 小结
1.AIDL (Android Interface Definition Language ) 2.AIDL 适用于 进程间通信,并且与Service端多个线程并发的情况,如果只是单个线程 可以使用 ...
- Android AIDL使用详解_Android IPC 机制详解
一.概述 AIDL 意思即 Android Interface Definition Language,翻译过来就是Android接口定义语言,是用于定义服务器和客户端通信接口的一种描述语言,可以拿来 ...
- 【Android学习】android:layout_weight的用法实例
对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...
- Android之Adapter用法总结-(转)
Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...
随机推荐
- spring启动后立即执行方法
1.方法所属的类继承InitializingBean接口. 2.重写afterPropertiesSet()方法. afterPropertiesSet方法会在bean被初始化时执行. 当bean的作 ...
- HTTP 压力测试工具
http_load 程序非常小,解压后也不到100K http_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载.但是它不同于大多数压力测试工 具,它可以以一个单一的进程运行,一般不会 ...
- linux命令之crontab定时执行任务【转】
本文转载自:https://www.cnblogs.com/coffy/p/5608095.html 一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护 ...
- CICD 基础
代码测试覆盖率 最近在负责相关插件的集成,今天第一次接触到"代码覆盖率"这个概念,那么,就做些简单的笔记吧. 好文 如何提高一个研发团队的"代码速度"? 代碼覆 ...
- Docker 配置阿里云镜像加速器
由于国内访问直接访问docker hub网速比较慢,拉取镜像的时间就会比较长.一般我们会使用镜像加速或者直接从国内的一些平台镜像仓库上拉取. 根据网上提供的方案,有网易,daocloud,ustc等解 ...
- springBoot 学习(总)
springBoot有个中文文档网址,应该是持续更新的. 别的资料 http://tengj.top/2017/02/26/springboot1/ http://412887952-qq-c ...
- Paper Reading: In Defense of the Triplet Loss for Person Re-Identification
In Defense of the Triplet Loss for Person Re-Identification 2017-07-02 14:04:20 This blog comes ...
- WebPack填坑笔记
loader使用时不需要用require引入,在使用plugins(插件)才需要使用require引入 压缩js代码会导致热更新失效 所以开发环境先不要进行压缩 给css加前缀的 postcss-lo ...
- SQL语句执行的顺序机制
From Where Group by Having Select 表达式 Distinct ORDER BY TOP/OFFSET-FETCH
- spring 配置ibatis和自动分页
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...