一、为什么需要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连接池的更多相关文章

  1. Android-Service基本用法、AIDL、Binder连接池详解

    本文介绍Service与Activity之间的通信,文章包含以下内容: 一.Service基本用法 二.通过AIDL实现Service与Activity跨进程通信 三.Binder连接池 四.使用Me ...

  2. 连接SQLServer时,因启用连接池导致孤立事务的原因分析和解决办法

    本文出处:http://www.cnblogs.com/wy123/p/6110349.html 之前遇到过这么一种情况: 连接数据库的部分Session会出现不定时的阻塞,这种阻塞时长时短,有时候持 ...

  3. C3p0连接池配置

    在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ②连接数据库(Connection co ...

  4. Java第三方数据库连接池库-DBCP-C3P0-Tomcat内置连接池

    连接池原理 数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”.预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去.我们可以通过设定连接池 ...

  5. common-pool2 学习:thrift连接池的另一种实现

    对象池是一种很实用的技术,经典的例子就是数据库连接池.去年曾经从零开始写过一个thrift客户端连接池.如果不想重造轮子,可以直接在apache开源项目commons-pool的基础上开发. 步骤: ...

  6. druid连接池获取不到连接的一种情况

    数据源一开始配置: jdbc.initialSize=1jdbc.minIdle=1jdbc.maxActive=5 程序运行一段时间后,执行查询抛如下异常: exception=org.mybati ...

  7. C3P0连接池配置和实现详解

    一.配置 <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> ...

  8. hibernate+mysql的连接池配置

    1:连接池的必知概念    首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...

  9. 连接池的实现 redis例子

    # -*- encoding:utf-8 -*- # import pymysql # # conn = pymysql.connect(host="127.0.0.1", por ...

随机推荐

  1. C++中vector和list排序

    容器.泛型算法.和类是不是就是C++相对于C"++"的那部分呢?暂时先这么认为吧.如果这篇博客有幸被别人看到,请帮忙指出.--C++ 菜鸟 留. vector的迭代器是随机访问迭代 ...

  2. Marineking wilyin

    A - Marineking wilyin Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Ot ...

  3. Max Num---hdu2071

    Max Num Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. MYSQL 维护表的常用 5 方法

    方法 1. analyze table: 本语句用于分析和存储表的关键字分布.在分析期间,使用一个读取锁定对表进行锁定.这对于MyISAM, BDB和InnoDB表有作用. 方法 2. CHECK T ...

  5. 阿里云 centos 修改iptables

    一.检查iptables服务状态 首先检查iptables服务的状态 [root@woxplife ~]# service iptables status iptables: Firewall is ...

  6. 【测试Json的多空格问题】

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. MySQL_数据分页查询(limit用法)

    取前5条数据 select * from table_name limit 0,5 或 select * from table_name limit 5 取第11条到第15条数据,共5条 select ...

  8. docker文章

    https://training.docker.com/self-paced-training http://special.csdncms.csdn.net/BeDocker/ http://clo ...

  9. Unix/Linux环境C编程入门教程(26) 字符数字那些事儿

    1.gcvt() strtod() strtol() strtoul() toascii() tolower() toupper函数介绍 gcvt(将浮点型数转换为字符串,取四舍五入) 相关函数 ec ...

  10. thrift TNonblockingServer 使用

    下载 0.9.1 版本 (0.9.2需要 2.5的bison,而 RHEL6上自带bison是2.4)   TNonblockingServer 时必须使用 TFramedTransport ,不能使 ...