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 ...
随机推荐
- thinkphp+redis实现秒杀功能
好久没来整理文章了,闲了没事写篇文章记录下php+redis实现商城秒杀功能. 1,安装redis,根据自己的php版本安装对应的redis扩展(此步骤简单的描述一下) 1.1,安装 php_igbi ...
- juce viewport使用
1.设置内容组件 void PropertyPanel::init() { messageWhenEmpty = TRANS("(nothing selected)"); addA ...
- 【转】mysql行列转换方法总结
转:http://blog.chinaunix.net/uid-7692530-id-2567582.html 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解 ...
- sqlserver2005仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 'SendMealAddress'中为标识列指定显式值。
ps = con.prepareStatement("insert into SendMealAddress values(null,?,?,?,?)"); 表有一列是自增长的标识 ...
- maven命令/依赖/聚合
一,编写pom.xml 首先我们看一下pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...
- ListView的小知识
1.设置项目分割功能 android:divider="@android:color/blue" android:dividerHeight="10dp" &l ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- Python2.7 转义和正则匹配中文
今天爬虫(新浪微博 个人信息页面)的时候遇到了转义和正则匹配中文出乱码的问题. 先给出要匹配的部分网页源代码如下: <span class=\"pt_title S_txt2\&quo ...
- ASP.NET MVC 中@html.ActionLink的几种参数格式
一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, ...
- EPiServer 简单项目总结
国内用到的EPiServer应该不多,所以记录点用到过的东西,以便分享 1.EPiServer office site http://www.episerver.com/ 2.EPiServer CM ...