一、为什么需要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. List小练习

    功能:创建链表节点,删除节点,顺序打印,不改变原结构的情况下分别用STL中的stack实现逆序打印和利用函数递归打印 代码如下: //链表问题struct ListNode {    int m_nV ...

  2. const的一些总结

    const的一些总结 采用const符号常量写出来的代码更容易维护,有些函数只读不写: 1 常变量: const 类型说明符 变量名 2 常引用: const 类型说明符 &变量名 3 常成员 ...

  3. 深入浅析mysql引擎

    mysql引擎 mysql数据库引擎取决于mysql在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译mysql.在缺省情况下,mysql支持三个引擎:ISAM,MYISAM和HEAP.另 ...

  4. windows 数据类型转换为 dotnet 数据类型

    Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR Char DOUB ...

  5. 移动网页版Meta 标签

    viewport 大部分移动浏览器都接受,比如 Opera Mobile, iPhone, Android, Iris, IE, BlackBerry, Obigo, Firefox 最基本的例子,在 ...

  6. thinkphp框架开启页面gzip压缩

    Thinkphp下开启gzip压缩很简单,不管你是哪个版本,只要在你的入口文件index.PHP中加入以下两行,如果你的服务器支持,那么就OK了. define ( "GZIP_ENABLE ...

  7. Hibernate 体系结构简述

    SessionFactory: Hibernate的关键对象,它是单个数据库映射关系经过编译后的内存镜像,同时它是线程安全的.它是生成Session的工厂,本身需要依赖于ConnectionProvi ...

  8. Ajax日记

    简要讲Ajax 的内容归纳为博客. AJAX能且仅能从服务器上读取文件,我们使用ajax的时候分为以下几个步骤: 1.  创建ajax对象 2.  链接服务器 3.  发送请求 4.  接收返回值 下 ...

  9. MEMS陀螺仪(gyroscope)的结构

    MEMS陀螺仪(gyroscope)的设计和工作原理可能各种各样,但是公开的MEMS陀螺仪均采用振动物体传感角速度的概念.利用振动来诱导和探测科里奥利力而设计的MEMS陀螺仪没有旋转部件.不需要轴承, ...

  10. VC调用javascript的几种方法

    第一种:通过execScript调用.这种方法,虽然操作方便,但不能获取返回值.m_spHtmlDoc->get_parentWindow(&m_pHtmlWindow);VARIANT ...