Binder连接池
一、为什么需要Binder线程池
产生原因:因为当有多个不同的业务块都要使用AIDL来进行通信,则需要创建多个Service,每创建一个Service就需要消耗系统资源。
解决思路:将所有的AIDL放在一个Service中处理
二、使用
具体原理:①、每个AIDL创建AIDL接口并用类实现此接口
②、然后创建一个主AIDL开放queryBinder接口,客户端输入int标识符来选择需要哪一个AIDL,来返回相对应的AIDL在服务器中的具体对象
③、服务器返回主AIDL类给客户端,这样客户端就能够调用主AIDL对象的queryBinder(int enum),获取需要的aidl
主要作用:将每个业务的AIDL请求统一转发给一个Service,避免Service的重建
具体使用:
前期准备:
假设有两个AIDL:
// ICompture.aidl
package com.chen.android.testbinderpool.aidl; // Declare any non-default types here with import statements
/*用来做加法计算的aidl*/
interface ICompture {
int add(int a,int b);
}
ICompute.aidl
// ISecurityCenter.aidl
package com.chen.android.testbinderpool.aidl; // Declare any non-default types here with import statements
/*写入账号,密码的aidl*/
interface ISecurityCenter {
String encrypt(String content);
String decrypt(String password);
}
ISecurityCenter.aidl
创建IBinderPool.aidl:
package com.chen.android.testbinderpool.aidl; // Declare any non-default types here with import statements
/*根据标识符,返回客户端需求的AIDL*/
interface IBinderPool {
IBinder queryBinder(int binderCode);
}
IBinderPool.aidl
使用:
创建类继承并重写两个aidl接口
import android.os.RemoteException; /**
* Created by PC on 2016/4/7.
*/
public class ComputeImpl extends ICompture.Stub {
@Override
public int add(int a, int b) throws RemoteException {
return a+b;
}
}
ComputeImpl.java
public class SecurityConterImpl extends ISecurityCenter.Stub{
private static final char SECRET_CODE = '^';
@Override
public String encrypt(String content) throws RemoteException {
char[] chars = content.toCharArray();
for (int i=0; i<chars.length; ++i){
chars[i] ^= SECRET_CODE;
}
return new String(chars);
}
@Override
public String decrypt(String password) throws RemoteException {
return encrypt(password);
}
}
SecurityCenterImpl.java
创建BinderPool连接池:
1.单例模式:整个app只能创建一个对象
2.创建IBinderPool的静态类:重写接口
3.创建该类时候,自动连接Service
4.创建queryBinder()方法,能够调用IBinderPool的queryBinder()(因为服务器返回的Binder在BinderPool中)
public class BinderPool {
//客户端通过标识符,获取相对应的Binder
public static final int BINDER_SECURITY = 0;
public static final int BINDER_COMPTURE = 1;
private static BinderPool sBinderPool;
private static Context mContext;
private IBinderPool mIBinderPool;
/*单例模式,在整个app中只会产生一个对象*/
private BinderPool(Context context) {
mContext = context.getApplicationContext();
connectService();
}
public static BinderPool getInstance(Context context){
synchronized (BinderPool.class) {
if (sBinderPool == null) {
sBinderPool = new BinderPool(context);
}
}
return sBinderPool;
}
/*当客户端创建该对象时候,自动连接Service,不用客户端再自己动手了*/
private void connectService(){
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//获取BinderPool对象
mIBinderPool = BinderPoolImpl.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
Intent intent = new Intent(mContext,BinderPoolService.class);
mContext.bindService(intent,connection,Context.BIND_AUTO_CREATE);
}
/*因为客户端没法收到从Service发送过来的Binder,利用该方法来执行Binder的方法*/
public IBinder queryBinder (int binderCode){
IBinder binder = null;
if (mIBinderPool != null){
try {
binder = mIBinderPool.queryBinder(binderCode);
} catch (RemoteException e) {
e.printStackTrace();
}
}
return binder;
}
/*继承IBinderPool接口,重写方法*/
public static class BinderPoolImpl extends IBinderPool.Stub{
@Override
public IBinder queryBinder(int binderCode) throws RemoteException {
IBinder binder;
switch (binderCode){
case BINDER_COMPTURE:
binder = new ComputeImpl();
break;
case BINDER_SECURITY:
binder = new SecurityConterImpl();
break;
default:
binder = null;
break;
}
return binder;
}
}
}
BinderPool
服务器端返回IBinderPool的Binder对象
public class BinderPoolService extends Service {
private IBinder mIBinder;
@Override
public void onCreate() {
super.onCreate();
//获取IBinderPool对象
mIBinder = new BinderPool.BinderPoolImpl();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mIBinder;
}
}
BinderPoolService
Binder连接池的更多相关文章
- Android-Service基本用法、AIDL、Binder连接池详解
本文介绍Service与Activity之间的通信,文章包含以下内容: 一.Service基本用法 二.通过AIDL实现Service与Activity跨进程通信 三.Binder连接池 四.使用Me ...
- 连接SQLServer时,因启用连接池导致孤立事务的原因分析和解决办法
本文出处:http://www.cnblogs.com/wy123/p/6110349.html 之前遇到过这么一种情况: 连接数据库的部分Session会出现不定时的阻塞,这种阻塞时长时短,有时候持 ...
- C3p0连接池配置
在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Class.forName("数据库驱动类");) ②连接数据库(Connection co ...
- Java第三方数据库连接池库-DBCP-C3P0-Tomcat内置连接池
连接池原理 数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”.预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去.我们可以通过设定连接池 ...
- common-pool2 学习:thrift连接池的另一种实现
对象池是一种很实用的技术,经典的例子就是数据库连接池.去年曾经从零开始写过一个thrift客户端连接池.如果不想重造轮子,可以直接在apache开源项目commons-pool的基础上开发. 步骤: ...
- druid连接池获取不到连接的一种情况
数据源一开始配置: jdbc.initialSize=1jdbc.minIdle=1jdbc.maxActive=5 程序运行一段时间后,执行查询抛如下异常: exception=org.mybati ...
- C3P0连接池配置和实现详解
一.配置 <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> ...
- hibernate+mysql的连接池配置
1:连接池的必知概念 首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...
- 连接池的实现 redis例子
# -*- encoding:utf-8 -*- # import pymysql # # conn = pymysql.connect(host="127.0.0.1", por ...
随机推荐
- 班上有学生若干名,已知每名学生的成绩(整数),求班上所有学生的平均成绩,保留到小数点后两位。同时输出该平均成绩整数部分四舍五入后的数值。 第一行有一个整数n(1<= n <= 100),表示学生的人数。其后n行每行有1个整数,表示每个学生的成绩,取值在int范围内。
#include<iostream> #include<iomanip> using namespace std ; int main() { int n; while(cin ...
- [转载]CodeBlocks+wxWidgets
到www.CodeBlocks.org下载并安装CodeBlocks,最好下载MinGW版本的,可以省掉安装和配置GCC的麻烦. 到www.wxWidgets.org下载并安装wxWidgets,如果 ...
- spring的IOC,DI及案例详解
一:spring的基本特征 Spring是一个非常活跃的开源框架:它是一个基于Core来架构多层JavaEE系统的框架,它的主要目的是简化企业开发.Spring以一种非侵入式的方式来管理你的代码,Sp ...
- [汇编语言]-第八章 div指令,伪指令dd,dup
1- div除法指令 (1) 除数: 有8位和16位两种,在一个寄存器或内存单元中. (2) 被除数: 默认放在AX和DX或AX中 除数为8位, 被除数为16位, 默认在AX中存放. 除数为16位, ...
- Doubles water!!!!!!只会水题怎么破
Doubles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- YUI的模块化开发
随着互联网应用越来越重,js代码越来越庞大,如何有效的去组织自己的代码,变得非常重要.我们应该学会去控制自己的代码,而不是到最后一堆bug完全不知道从哪冒出来.前端的模块化开发可以帮助我们有效的去管理 ...
- 最核心4大NFC技术规范详解
1998年,飞利浦.索尼和诺基亚创建了 NFC论坛,宗旨是推动NFC的发展普及和规范化.论坛至今共推出了5大类技术规范:协议技术规范(Protocol Technical Specification) ...
- C++类对应的内存结构
提示1:对“内存结构”表示有疑问或不解的,先参考: http://blog.csdn.net/guogangj/archive/2007/05/25/1625199.aspx, 本文使用的表示方法和V ...
- hdu 1150 Machine Schedule hdu 1151 Air Raid 匈牙利模版
//两道大水……哦不 两道结论题 结论:二部图的最小覆盖数=二部图的最大匹配数 有向图的最小覆盖数=节点数-二部图的最大匹配数 //hdu 1150 #include<cstdio> #i ...
- C编程技巧
1,attempted assighnment to literal if (i == 3) { //codes } else if (4 == 4); 2,引用数组元素相当于对指针加上偏移量的引用 ...