Binder基本使用
Android开发中,Binder是一种跨进程通信方式,而使用AIDL可以实现Binder的工作。
如何使用它是了解它的第一步,本文章主要记录使用Binder的一些步骤。(代码思路参考《Android开发艺术探索》任玉刚 著)
1.创建两个activity
两个activity(OneActivity、TwoActivity),将OneActivity假设为服务端,TwoActivity假设为客户端,分别运行在不同进程中
在AndroidManifest.xml中,为TwoActivity设置进程,这样两个activity就分别运行在不同的进程中了
<activity android:name=".TwoActivity" android:process=":test"/>
2. 创建AIDL文件
在AIDL文件中声明客户端想要调用服务端的方法
interface IInfManager {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void setName(String name);
String getName();
}
AIDL文件声明完,activity等文件并不能调用到IInfManager接口,需要在app的build.gradle文件中的android{}中添加
sourceSets{
main{
java.srcDirs = ['src/main/java', 'src/main/aidl']
}
}
然后点击sync now按钮,activity文件就可以调用到IInfManager接口了,可以在app\build\generated\source\aidl\debug文件下找到自动生成的IInfManager.java文件。
3.创建Service
Service中创建Binder对象,在onBind方法中返回这个对象,Binder对象中具体实现了IInfManager接口中的方法。Service需要在AndroidManifest.xml中注册。
public class InfManageService extends Service{
private String name;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
name = intent.getStringExtra("name");
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
private Binder binder = new IInfManager.Stub() {
@Override
public void setName(String mName) throws RemoteException {
name = mName;
}
@Override
public String getName() throws RemoteException {
return name;
}
};
}
4.服务端OneActivity
OneActivity中设置按钮跳转至TwoActivity,这里为了简单,使用startService可以为InfManageService中name变量初始化"zhangsan"的值。也可以与客户端TwoActivity中一样,绑定service,建立连接,来设置name的值(具体参考下一步客户端的用法)。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
Intent intent = new Intent(OneActivity.this, InfManageService.class);
intent.putExtra("name", "zhangsan");
startService(intent);
btn_one_gototwo = (Button) findViewById(R.id.btn_one_gototwo);
btn_one_gototwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(OneActivity.this, TwoActivity.class);
startActivity(intent);
}
});
}
5.客户端TwoActivity
首先绑定InfManageService服务,建立连接,连接成功后通过返回的IBinder对象可以获得IInfManager接口,可以通过这个接口去使用服务端的方法。
private TextView tv_two_name;
private Button btn_two_change;
private IInfManager iInfManager;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iInfManager = IInfManager.Stub.asInterface(service);
try {
tv_two_name.setText(iInfManager.getName());
Log.i("TwoActivity","first:" + iInfManager.getName());
iInfManager.setName("lisi");
Log.i("TwoActivity","next:" + iInfManager.getName());
}catch (RemoteException e){
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
tv_two_name = (TextView) findViewById(R.id.tv_two_name);
btn_two_change = (Button) findViewById(R.id.btn_two_change);
Intent intent = new Intent(TwoActivity.this, InfManageService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
btn_two_change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
iInfManager.setName("wangmazi");
tv_two_name.setText(iInfManager.getName());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
上面代码onServiceConnected方法中,首先在TwoActivity界面中显示了服务端的name变量内容"zhangsan"
Binder基本使用的更多相关文章
- 笔记:Binder通信机制
TODO: 待修正 Binder简介 Binder是android系统中实现的一种高效的IPC机制,平常接触到的各种XxxManager,以及绑定Service时都在使用它进行跨进程操作. 它的实现基 ...
- Binder in Java
Android在Native层实现了进程间的Binder通信,但是上层应用程序的开发及Framework的实现都是Java,用Java层再实现一次肯定是不合理的,Java可以通过JNI调用Native ...
- Binder In Native
关于Binder的设计思想与Driver层实现细节可以看这个:Android Binder设计与实现 - 设计篇,这里首先简要概括一下. Service的每个Binder实体位于Service所属的进 ...
- [转]Android Binder设计与实现 - 设计篇
摘要 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥有管道,system V IPC,socket等IPC手段,却还要倚赖Binder来实现进程间通信,说明Binder ...
- 用C++开发Binder服务
用C++来实现Binder服务比较麻烦,原因是没有AIDL的辅助,必须手工来写中间的代码. 首先写一个服务类ExampleServer的代码: class ExampleServer : public ...
- C++实例讲解Binder通信
binder是android里面的通信机制,这就不说它如何如何好了,Goog已经说过了,这里不多说.binder是一个面向对象的编程方法,大量使用虚函数类.最近研究binder看到一网友写的,就借鉴一 ...
- 为什么使用Binder而不是其他IPC机制
本文搬运自:Advantages of using Binder for IPC in Android 使用Binder而不是其他(Semaphores , Message Queue, PIPES) ...
- C++使用binder实例
Android系统最常见也是初学者最难搞明白的就是Binder了,很多很多的Service就是通过Binder机制来和客户端通讯交互的.所以搞明白Binder的话,在很大程度上就能理解程序运行的流程. ...
- LocalBroadcastManager 的实现原理,Handler还是 Binder?
原文: http://www.trinea.cn/android/localbroadcastmanager-impl/ 对 LocalBroadcastManager 大家应该都不陌生,相对 Bro ...
- 浅谈android binder机制
binder机制 是谷歌优化在android上更适合终端的IPC(多进程通信方式),满足系统对通信方式,传输性能和安全性的要求. 特性: 1. 用驱动程序来推进进程间的通信.2. 通过共享内存来提高性 ...
随机推荐
- mysql中更改字符集为utf8&&mysql中文输入不了问题解决
写给TT:对不起啦!! 嗯,输入不了中文,大多数问题是mysql的字符集设置的问题,当然,别的问题也有可能, 这里我们用两种方法设置mysql的字符集,图形化工具和命令行的方式(一种操作完即可) 一, ...
- 【转】document.form.action,表单分向提交
document.form.action,表单分向提交,javascript提交表单 同一个表单可以根据用户的选择,提交给不同的后台处理程序.即,表单的分向提交.如,在编写论坛程序时,如果我们希望实现 ...
- 《高性能MySQL》之MySQL查询性能优化
为什么查询会慢? 响应时间过长.如果把查询看做是一个任务,那么它由一系列子任务组成,每个子任务都会消耗一定的时间.如果要优化查询,实际上优化其子任务,要么消除其中一些子任务,要么减少子任务的执行次数, ...
- Thumb.db看不到的问题
今天读取数据集的时候总是会读到一个Thumb.db的缩略图文件,点开查看选项里面的显示隐藏文件.文件夹处于勾选状态,此时文件夹中并不存在此文件. 解决方案: 勾选掉隐藏受保护的操作系统文件即可.
- Wireshark入门与进阶系列五之常见捕获过滤器
0x00 前言 我们都知道,wireshark可以实现本地抓包,同时Wireshark也支持remote packet capture protocol(rpcapd)协议远程抓包,只要在远程主机上安 ...
- ios---CoreLocation框架实现定位功能
CoreLocation框架实现定位功能(iOS8.0之后) // // ViewController.m // 定位 // // Created by admin on 2017/9/20. // ...
- 尼姆博弈+SG函数
博弈这个东西真的很费脑诶.. 尼姆博奕(Nim Game):游戏者轮流从一堆棋子(或者任何道具)中取走一个或者多个,最后不能再取的就是输家.当指定相应数量时,一堆这样的棋子称作一个尼姆堆 当n堆棋子的 ...
- Oracle批量创建同义词
一.介绍 Oracle的同义词(synonyms)从字面上理解就是别名的意思,和视图的功能类似,就是一种映射关系.它可以节省大量的数据库空间,对不同用户的操作同一张表没有多少差别;它扩展了数据库的使用 ...
- python实现ip地址的包含关系判断
python的IPy模块虽然可以实现一些ip地址的判断,但是不是很完美,有些场景根本判断不出来,还会抛出异常,比如一个地址范围和一个ip/掩码,这种不同类型就无法判断. 对此通过自己写函数来实现ip地 ...
- 关于Influxdb1.4.2在windows下的安装过程的一些问题的记录
一.安装与配置: 1. Influxdb在1.3以后版本已经关闭了内置 的8086的web管理功能,需要单独的工具来管理 2.其配置文件默认路径是linux格式,需要修改为本机windows格式 我的 ...