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 ...
随机推荐
- ODAC(V9.5.15) 学习笔记(十三)TOraMetaData
通过TOraMetaData控件获取Oracle数据库对象信息,首先需要设置MetaDataKind属性,然后设置Restrictions属性设置条件,最后通过激活数据集获取信息,演示代码如下: Me ...
- Spring Boot 项目中常见注解
@Autowired 自动导入依赖的 Bean.byType方式.把配置好的 Bean拿来用,完成属性.方法的组装,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作 import org ...
- HDU 4825 Xor Sum(01字典树)题解
思路:先把所有数字存进字典树,然后从最高位贪心. 代码: #include<set> #include<map> #include<stack> #include& ...
- 【教程】Git在Eclipse中的安装和基本使用
一.安装 点击 Help->Install New Software->add 安装地址为:http://download.eclipse.org/egit/updates/ 选择插件 ...
- jquery里面获取div区块的宽度与高度
https://blog.csdn.net/ll641058431/article/details/52768825 获取宽度 $('div').width(); 获取:区块的本身宽度 $(' ...
- 1、Ansible简介及简单安装、使用
参考Ansible权威指南:https://ansible-tran.readthedocs.io/en/latest/index.html 以下内容学习自马哥教育 Ansible: 运维工作:系统安 ...
- HDU 4318 Power transmission(最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=4318 题意: 给出运输路线,每条路线运输时都会损失一定百分比的量,给定起点.终点和初始运输量,问最后到达终点时最 ...
- web开发测试注意点
1.用户操作多页面情况 如果用session来获取当前页面情况时要特别注意,操作时出现另一个页面的情况,会出现传参数混乱 解决:后台可以获取并比对判断当前页面某些参数值
- JMeter 生成精度度为分钟的时间戳文件名
import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import java.text.Simple ...
- 【Python】【socket】
[server.py] """#练习1import socketimport threading sock = socket.socket()sock.bind(('12 ...