跨进程通信可以用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 语言用法的更多相关文章

  1. Android AIDL的用法

    一.什么是AIDL服务   一般创建的服务并不能被其他的应用程序访问.为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Cal ...

  2. Android webservice的用法详细讲解

    Android webservice的用法详细讲解 看到有很多朋友对WebService还不是很了解,在此就详细的讲讲WebService,争取说得明白吧.此文章采用的项目是我毕业设计的webserv ...

  3. Android aidl Binder框架浅析

      转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38461079 ,本文出自[张鸿洋的博客] 1.概述 Binder能干什么?B ...

  4. AIDL/IPC Android AIDL/IPC 进程通信机制——超具体解说及使用方法案例剖析(播放器)

    首先引申下AIDL.什么是AIDL呢?IPC? ------ Designing a Remote Interface Using AIDL 通常情况下,我们在同一进程内会使用Binder.Broad ...

  5. (转载)你真的理解Android AIDL中的in,out,inout么?

    前言 这其实是一个很小的知识点,大部分人在使用AIDL的过程中也基本没有因为这个出现过错误,正因为它小,所以在大部分的网上关于AIDL的文章中,它都被忽视了——或者并没有,但所占篇幅甚小,且基本上都是 ...

  6. Android AIDL 小结

    1.AIDL (Android Interface Definition Language ) 2.AIDL 适用于 进程间通信,并且与Service端多个线程并发的情况,如果只是单个线程 可以使用 ...

  7. Android AIDL使用详解_Android IPC 机制详解

    一.概述 AIDL 意思即 Android Interface Definition Language,翻译过来就是Android接口定义语言,是用于定义服务器和客户端通信接口的一种描述语言,可以拿来 ...

  8. 【Android学习】android:layout_weight的用法实例

    对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...

  9. Android之Adapter用法总结-(转)

    Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...

随机推荐

  1. 如何清除浮动塌陷? float:left 塌陷

    参考文章: http://www.jb51.net/css/502268.html 原文链接:http://www.jianshu.com/p/a0500b54da13 只要给li的宽度, 规定得小一 ...

  2. Maven集成Tomcat插件

    目录 类似插件及版本区别: 本地运行,启动嵌入式tomcat: 错误一: 错误二: Idea运行调试: vscode运行调试: 远程部署: 项目中的pom.xml配置: Tomcat中的tomcat- ...

  3. 更新32位Spyder从3.0.0-> 3.2.3

    https://stackoverflow.com/questions/51222550/how-to-update-spyder-3-3-0 It works!! 1. went to the An ...

  4. 【做题】UVA-12304——平面计算集合六合一

    可真是道恶心题-- 首先翻译一下6个任务: 给出一个三角形,求它的外界圆. 给出一个三角形,求它的内接圆. 给出一个圆和一个点,求过这个点的切线的倾斜角\(\alpha \in [0,180)\).( ...

  5. sql 之 INSERT IGNORE

    INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据.这样就可以保留 ...

  6. BZOJ2306: [Ctsc2011]幸福路径

    Description 有向图 G有n个顶点 1, 2, -, n,点i 的权值为 w(i).现在有一只蚂蚁,从 给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条 边,它 ...

  7. Activit工作流学习例子

    看了网上一些文章,动手操作了一遍,终于学会了Activit的一些常规使用. 一.Eclipse中的Activiti插件安装 Activiti有一个Eclipse插件,Activiti Eclipse ...

  8. P3980 [NOI2008]志愿者招募

    思路 巧妙的建图 因为每个志愿者有工作的时段,所以考虑让一个志愿者的流量能够从S流到T产生贡献 所以每个i向i+1连INF-a[x]的边(类似于k可重区间集),每个si向ti连边cap=INF,cos ...

  9. PTA 7-3 jmu-ds-单链表的基本运算(15 分)

    jmu-ds-单链表的基本运算(15 分) 实现单链表的基本运算:初始化.插入.删除.求表的长度.判空.释放.(1)初始化单链表L,输出L->next的值:(2)依次采用尾插法插入元素:输入分两 ...

  10. Latex: 减少图与文字之间的空白间隙

    参考: Remove space after figure and before text Latex: 减少图与文字之间的空白间隙 论文中图与文字之间的空白间隙过大,导致排版不大美观.解决方法是在\ ...