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 ...
随机推荐
- PO VO DAO DTO BO TO概念与区别
O/R Mapping 是 Object Relational Mapping(对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定,用对象来表示关系数据.在O/R Mapping的世界里,有两 ...
- 关于serialVersionUID的说明
1.为什么要使用serialVersionUID (1)对于实现了Serializable接口的类,可以将其序列化输出至磁盘文件中,同时会将其serialVersionUID输出到文件中. (2)然后 ...
- JQuery>>>get/post赋值全局变量
JQuery' post&get: asynchronous. 2014-06-27 16:57:25 var client; function getClient(){ $.ajax ...
- 小端存储(little Endian)大端存储(big Endian)
小端存储--低内存低字节 87654321 字节或半字节的最低位字节(Lowest Significant Bit,LSB)存放于内存最低位字节地址上.即最低地址存放的最低字节,为Power PC,I ...
- CentOS下安装Nginx并添加nginx_upload_module
安装前,最好能保证依赖的系统软件已经升级. yum update CentOS上安装Nginx,如果只是简单安装,不附加其他第三方模块,一句话可以搞定: yum install nginx ...
- struts2配置文件struts.xml的简介
本文在于总结,深入研究有别人写的很好了,也没必要再去写,将在本文后面附上他们的文章地址: 一.struts2的执行过程: 二.struts2的配置文件struts.xml 下面是其三大部分includ ...
- Ubuntu10.4 Install DB2V9.5
1. Download the DB2V9.5 Software from URL: http://www14.software.ibm.com/webapp/download/search.jsp ...
- 浅谈标签构建——TagBuilder
在很多项目中,可能我们需要写一些通用的控件标签,今天来简单的学习一下吧. 在前文中已经学习了 如何自定义MVC控件标签 ,感兴趣的朋友可以去看看. 今天主要还是讲解一下TagBuilder 我们打开源 ...
- JavaEE Tutorials (27) - Java EE的并发工具
27.1并发基础427 27.1.1线程和进程42827.2并发工具的主要组件42827.3并发和事务42927.4并发和安全43027.5jobs并发示例430 27.5.1运行jobs示例4302 ...
- C#操作XML的完整例子——XmlDocument篇(转载,仅做学习之用)
原文地址:http://www.cnblogs.com/serenatao/archive/2012/09/05/2672621.html 这是一个用c#控制台程序下, 用XmlDocument 进 ...