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基本使用的更多相关文章

  1. 笔记:Binder通信机制

    TODO: 待修正 Binder简介 Binder是android系统中实现的一种高效的IPC机制,平常接触到的各种XxxManager,以及绑定Service时都在使用它进行跨进程操作. 它的实现基 ...

  2. Binder in Java

    Android在Native层实现了进程间的Binder通信,但是上层应用程序的开发及Framework的实现都是Java,用Java层再实现一次肯定是不合理的,Java可以通过JNI调用Native ...

  3. Binder In Native

    关于Binder的设计思想与Driver层实现细节可以看这个:Android Binder设计与实现 - 设计篇,这里首先简要概括一下. Service的每个Binder实体位于Service所属的进 ...

  4. [转]Android Binder设计与实现 - 设计篇

    摘要 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥有管道,system V IPC,socket等IPC手段,却还要倚赖Binder来实现进程间通信,说明Binder ...

  5. 用C++开发Binder服务

    用C++来实现Binder服务比较麻烦,原因是没有AIDL的辅助,必须手工来写中间的代码. 首先写一个服务类ExampleServer的代码: class ExampleServer : public ...

  6. C++实例讲解Binder通信

    binder是android里面的通信机制,这就不说它如何如何好了,Goog已经说过了,这里不多说.binder是一个面向对象的编程方法,大量使用虚函数类.最近研究binder看到一网友写的,就借鉴一 ...

  7. 为什么使用Binder而不是其他IPC机制

    本文搬运自:Advantages of using Binder for IPC in Android 使用Binder而不是其他(Semaphores , Message Queue, PIPES) ...

  8. C++使用binder实例

    Android系统最常见也是初学者最难搞明白的就是Binder了,很多很多的Service就是通过Binder机制来和客户端通讯交互的.所以搞明白Binder的话,在很大程度上就能理解程序运行的流程. ...

  9. LocalBroadcastManager 的实现原理,Handler还是 Binder?

    原文: http://www.trinea.cn/android/localbroadcastmanager-impl/ 对 LocalBroadcastManager 大家应该都不陌生,相对 Bro ...

  10. 浅谈android binder机制

    binder机制 是谷歌优化在android上更适合终端的IPC(多进程通信方式),满足系统对通信方式,传输性能和安全性的要求. 特性: 1. 用驱动程序来推进进程间的通信.2. 通过共享内存来提高性 ...

随机推荐

  1. linux操作系统下调试python代码方法

    一.python有调试工具pdb,可以用来进行代码调试. pdb的常用命令说明: l #查看运行到哪行代码 n #单步运行,跳过函数 s #单步运行,可进入函数 p 变量 #查看变量值 b 行号 #断 ...

  2. mongo 查询 距离 某个点 多少 米距离 感谢 提供的数据。 感谢 mvc的 demo 。反正 就是各种感谢 文档之类的。

    昨天 去面试来着, 问了一下mong . 我记得mong支持 地理位置索引的,说了一下. 然后 面试官说 查询某个点 的 多少米范围, 这个该怎么实现? 我懵逼了.... 回去 查询了一下. 发现有 ...

  3. Dynamics 365 CRM Connected Field Service 不能接收IoT Alert

    今天浪费了2,3个小时再connected field service(CFS)上面. 状况如下 1. 在CFS中添加了新的customer assets,并且点击了注册按钮. 2. 注册步骤一直在i ...

  4. 了解人工智能?-百度AI

    了解人工智能? 什么是人工智能? 由人创造的"智慧能力",同样具备智慧生物的能力 耳朵=倾听=麦克风=语音识别 ASR Automatic Speech Recognition 嘴 ...

  5. Day4-Python3基础-装饰器、迭代器

    今日内容: 1.高阶函数 2.嵌套函数 3.装饰器 4.生成器 5.迭代器 1.高阶函数 定义: a:把一个函数名当作实参传给函数 a:返回值包含函数名(不修改函数的调用方式) import time ...

  6. Shell之作业控制

    命令 含义 jobs 列出所有正在运行的作业 ^Z(Ctrl+z) 暂停作业 bg 启动被暂停的作业 fg 将后台作业调到前台 kill 向指定作业发送kill信号 nohup 忽略所有发送给子命令的 ...

  7. 逆向番茄社区app的rsa加密方式

    Parse RSA public and private key pair from string in Java 逆向某APP,发现其大部分配置文件都是加密的 .所以逆向算法并解密 RSA和AES密 ...

  8. css的选择器及它的种类特性?

    今天主要说的是选择器的基础, 首先看,选择器的优先级:!important > 行间样式 > id选择器 > class 选择器 == 属性选择器 > 标签选择器 > 通 ...

  9. 简单处理IP XML数据

    ///* 编译环境: visual c++ */ //#include <stdio.h> //#include <winsock2.h> //#pragma comment( ...

  10. HDU_5692_dfs序+线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=5692 这道题真的是看了题解还搞了一天,把每条路径后序遍历按1-n重新标号,储存每个点在哪些路径中出现过(l和r数 ...