android即时消息处理机制
在android端做即时消息的时候。遇到的坑点是怎么保证消息即时性,又不耗电。为什么这么说呢?
pull定时轮询机制,比較浪费server资源;pushserver推送机制,须要保持长连接,client和server都要求比較高(网络环境,server保持连接数等),它们的具体优缺点不描写叙述了。上面这两种机制都要求client长期处于活动状态。前提是cpu处于唤醒状态,而android端有休眠机制,保证手机在大部分时间里都是处于休眠,减少耗电量,延长机时间。
手机休眠后,线程处理暂停状态,这样前面说的两种方式,都会处于暂停状态,从而导致休眠后就无法收消息问题。可能有人说手机有唤醒机制。假设一直唤醒呢,这样导致做的软件是耗电大户,基本不要一天手机电量就被干光,想想睡觉前有半格电,早上起来电量干光被关机,郁闷的心情顿时油然而生,所以这样干是不行的,会直接导致软件被卸载。
一、唤醒机制
假设要确保异步之外的事情做完,就得申请WakeLock,确保手机不休眠,不然事情干得一半,手机就休眠了。
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, (triggerAtTime + time), pi);
2、闹铃时间优化
public class AlarmTime {
public static final AtomicLong alarmTime=new AtomicLong(0);
/**
* 初始化闹铃时间,重连或者收到消息初始化一下
*/
public static long initAlarmTime(){
alarmTime.set(Global.ALARM_TRIGGER_TIME);
return alarmTime.get();
}
/**
* 优化闹铃时间。重连错误数超过一定次数。优化闹铃时间再尝试重连到错误数
* 10分钟,30秒、30秒、;;。。到达错误数,10分钟 。;;;。
* @return
*/
public static long optimizeAlarmTime(){
alarmTime.set(Global.ALARM_TRIGGER_OPTIMIZE_TIME);//10分钟
return alarmTime.get();
}
public static long incrementTime(){
long time =alarmTime.get();
if(time==0)
return alarmTime.addAndGet(Global.ALARM_TRIGGER_TIME);//默认30秒開始
else if(time<Global.ALARM_TRIGGER_MAX_TIME)//25分钟
return alarmTime.addAndGet(Global.ALARM_TRIGGER_TIME_INCREMENT);//每次递增5秒
else
return time;
}
}
3、唤醒机制
public final class IMWakeLock {
private static final String TAG = IMWakeLock.class.getSimpleName();
private WakeLock wakeLock = null;
private String tag="";
private PowerManager pm;
public IMWakeLock(Context paramContext,String tag){
this.tag =tag;
pm= ((PowerManager) paramContext.
getSystemService(Context.POWER_SERVICE));
wakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK , tag);
}
/**
* 获取电源锁,保持该服务在屏幕熄灭时仍然获取CPU时,保持执行
*/
public synchronized void acquireWakeLock() {
if(!pm.isScreenOn()) {
if (null != wakeLock&&!wakeLock.isHeld()) {
ImLog.d(TAG, tag+"@@===>获取唤醒休眠锁");
wakeLock.acquire();
}
}
}
/**
* 释放设备电源锁
*/
public synchronized void releaseWakeLock() {
if (null != wakeLock && wakeLock.isHeld()) {
ImLog.d(TAG, tag+"@@===>释放唤醒休眠锁");
wakeLock.release();
}
}
public synchronized void finalize(){
if (null != wakeLock && wakeLock.isHeld()) {
ImLog.d(TAG, tag+"@@===>释放唤醒休眠锁");
wakeLock.release();
}
wakeLock = null;
}
public boolean isScreenOn(){
return pm.isScreenOn();
}
}
4、唤醒时机
private void startApNotify(){
if(this.sessionID==0||this.ticket==null)
return;
if(wakeLock.isScreenOn()){
ImLog.d(TAG, "NotifyService@@===>启动空请求");
apNotifyThread=new ApNotifyThread(this,false);
}else{
wakeLock.acquireWakeLock();
apNotifyThread=new ApNotifyThread(this,true);
}
exec=Executors.newSingleThreadExecutor();
exec.execute(apNotifyThread);
exec.shutdown();
}
二、消息收取
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FnY3k=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
public class ApNotifyThread extends Thread{
private static final String TAG = ApNotifyThread.class.getSimpleName();
protected volatile boolean isRunning=false;
protected volatile APHold.Client client;
protected volatile VRVTHttpClient thc;
protected volatile TProtocol protocol;
protected volatile long sessionID;
protected volatile String ticket;
protected final long ERRORNUM=15;
protected NotifyService service;
protected boolean isOld=false;
protected boolean isDoShortRequest=false;
public ApNotifyThread(NotifyService service,boolean isDoShortRequest){
this.sessionID=service.getSessionID();
this.ticket=service.getTicket();
this.service=service;
this.isDoShortRequest=isDoShortRequest;
}
@Override
public void run(){
ImLog.d(TAG, "ApNotifyThread@@===>空请求開始处理 threadID="+Thread.currentThread().getId());
this.isRunning=true;
if(this.isDoShortRequest){
if(shortEmptyRequest()&&this.isRunning)
longEmptyRequest(); //再开启长空请求
}else{
longEmptyRequest();
}
ImLog.d(TAG, "ApNotifyThread@@===>"+(this.isOld?"上一个":"")+"空请求终止 threadID="+Thread.currentThread().getId());
this.isRunning=false;
}
/**
* 初始化
* @param isLongTimeOut
* @throws Exception
*/
private void init(boolean isLongTimeOut) throws Exception{
thc= NotifyHttpClientUtil.getVRVTHttpClient(isLongTimeOut);
protocol = new TBinaryProtocol(thc);
}
/**
* 长空请求
*/
private void longEmptyRequest(){
try{
this.init(true);
client= new APHold.Client(protocol);
for (;;) {
if(!NetStatusUtil.havActiveNet(IMApp.getApp())){
ImLog.d(TAG, "longEmptyRequest@@===>无可用网络");
break;
}
try {
if(!handleMessage())
break;
} catch (TException e) {
if(!this.isRunning)
break;
ImLog.d(TAG, "longEmptyRequest@@===>发请求异常:"+ e.getMessage());
if(exceptionHandler(e)){
throw new IMException("连接失败次数过多",MessageCode.IM_EXCEPTION_CONNECT);
}
continue;
}
}
ImLog.d(TAG, "longEmptyRequest@@===>"+(this.isOld?"上一个":"")+"空请求正常退出");
} catch (Exception e) {
ImLog.d(TAG, "longEmptyRequest@@===>"+(this.isOld?"上一个":"")+"空请求异常退出"+e.getMessage());
if (exceptionHandler(e)) {
// 调用重连
ImLog.d(TAG, "longEmptyRequest@@===>调用重连");
this.service.getDataSyncer().setValue(UserProfile.RECONNECT, "0");
}
}finally{
close();
}
}
/**
* 短空请求
* @return
*/
private boolean shortEmptyRequest(){
boolean isDoLongRequest=true;
try{
long messageNum=0;
if(!NetStatusUtil.havActiveNet(IMApp.getApp())){
ImLog.d(TAG, "shortEmptyRequest@@===>无可用网络");
return false;
}
this.init(false);
//获取消息数
APService.Client apclient = new APService.Client(protocol);
this.service.getDataSyncer().setValue(UserProfile.LASTREQUESTTIME, String.valueOf(SystemClock.elapsedRealtime()));
ImLog.d(TAG, "shortEmptyRequest@@===>notifyID:"+NotifyID.notifyID.get());
messageNum= apclient.getNotifyMsgSize(sessionID, ticket, NotifyID.notifyID.get());
NotifyError.notifyErrorNum.set(0);
ImLog.d(TAG, "shortEmptyRequest@@===>获取消息条数:"+messageNum);
if(messageNum==-1)
throw new IMException("session 失效",MessageCode.IM_BIZTIPS_SESSIONINVAILD);
//假设有消息接收消息
if(messageNum>0&&this.isRunning){
long receiveMessageNum=0;
client= new APHold.Client(protocol);
for (;;) {
if(!NetStatusUtil.havActiveNet(IMApp.getApp())){
ImLog.d(TAG, "shortEmptyRequest@@===>无可用网络");
break;
}
if(!handleMessage())
break;
receiveMessageNum++;
if(receiveMessageNum==messageNum) //短连接接收完后退出
break;
}
}
ImLog.d(TAG, "shortEmptyRequest@@===>"+(this.isOld?
"上一个":"")+"空请求正常退出");
}catch(Exception e){
ImLog.d(TAG, "shortEmptyRequest@@===>"+(this.isOld?"上一个":"")+"空请求异常退出"+e.getMessage());
if(exceptionHandler(e)){
isDoLongRequest=false;
//调用重连
ImLog.d(TAG, "shortEmptyRequest@@===>调用重连");
this.service.getDataSyncer().setValue(UserProfile.RECONNECT, "0");
}
}
finally{
close();
this.service.releaseWakeLock();
}
return isDoLongRequest;
}
/**
* 异常处理 推断是否重连
* @param e
* @return
*/
private boolean exceptionHandler(Exception e){
boolean isReconnect=false;
if ( e instanceof IMException) {
isReconnect=true;
}else if (!(e instanceof SocketTimeoutException)&&!(e instanceof NoHttpResponseException)) {
NotifyError.notifyErrorNum.incrementAndGet();
if(NotifyError.notifyErrorNum.get()>this.ERRORNUM){
isReconnect=true;
NotifyError.notifyErrorNum.set(0);
}
}else
NotifyError.notifyErrorNum.set(0);
e.printStackTrace();
return isReconnect;
}
/**
* 空请求发送和接收数据处理
* @throws TException
*/
private boolean handleMessage() throws TException{
if(!this.isRunning)
return false;
ImLog.d(TAG, "handleMessage@@===>sessionID "+sessionID);
SendEmptyRequestReq req = new SendEmptyRequestReq();
req.setSessionID(sessionID);
req.setTicket(ticket);
req.setNotifyID(NotifyID.notifyID.get());
ImLog.d(TAG, "handleMessage@@===>一次空请求周期開始 ");
this.service.getDataSyncer().setValue(UserProfile.LASTREQUESTTIME, String.valueOf(SystemClock.elapsedRealtime()));
client.SendEmptyRequest(req);
NotifyError.notifyErrorNum.set(0);
if(!this.isRunning)
return false;
APNotifyImpl iface = new APNotifyImpl();
APNotify.Processor<Iface> processor = new APNotify.Processor<Iface>(iface);
boolean isStop = false;
while (!isStop) {
try {
ImLog.d(TAG, "handleMessage@@===>进入接收数据处理");
while (processor.process(protocol,
protocol) == true) {
isStop = true;
break;
}
ImLog.d(TAG, "handleMessage@@===>结束接收数据处理");
} catch (TException e) {
ImLog.d(TAG, "handleMessage@@===>接收数据处理异常");
isStop = true;
}
}
ImLog.d(TAG, "handleMessage@@===>一次空请求周期结束");
if(!iface.isSessionVaild){//后台报session 失效
this.service.setSessionID(0);
this.service.setTicket(null);
return false;
}
//重设闹铃
this.service.getDataSyncer().setValue(UserProfile.ALARM_TTIME, "0");
return true;
}
/**
* 关闭连接
*/
private void close() {
synchronized(this){
if (thc != null) {
thc.shutdown();
thc.close();
thc=null;
}
}
if (client != null && client.getInputProtocol() != null) {
client.getInputProtocol().getTransport().close();
client.getOutputProtocol().getTransport().close();
}
}
/**
* 线程中断
*/
public void interrupt() {
this.isRunning=false;
this.isOld=true;
close();
super.interrupt();
}
/**
* 推断是否在执行状态
*/
public boolean isRunning(){
return isRunning;
}
android即时消息处理机制的更多相关文章
- 转 Android的消息处理机制(图+源码分析)——Looper,Handler,Message
作为一个大三的预备程序员,我学习android的一大乐趣是可以通过源码学习google大牛们的设计思想.android源码中包含了大量的设计模式,除此以外,android sdk还精心为我们设计了各种 ...
- 【转】android的消息处理机制(图+源码分析)——Looper,Handler,Message
原文地址:http://www.cnblogs.com/codingmyworld/archive/2011/09/12/2174255.html#!comments 作为一个大三的预备程序员,我学习 ...
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
android源码中包含了大量的设计模式,除此以外,android sdk还精心为我们设计了各种helper类,对于和我一样渴望水平得到进阶的人来说,都太值得一读了.这不,前几天为了了解android ...
- 《Android进阶》之第三篇 深入理解android的消息处理机制
Android 异步消息处理机制 让你深入理解 Looper.Handler.Message三者关系 android的消息处理机制(图+源码分析)——Looper,Handler,Message an ...
- Android异步消息处理机制(多线程)
当我们需要执行一些耗时操作,比如说发起一条网络请求时,考虑到网速等其他原因,服务器未必会立刻响应我们的请求,如果不将这类操作放在子线程里去执行,就会导致主线程被阻塞住,从而影响用户对软件的正常使用. ...
- 【转载】Android异步消息处理机制详解及源码分析
PS一句:最终还是选择CSDN来整理发表这几年的知识点,该文章平行迁移到CSDN.因为CSDN也支持MarkDown语法了,牛逼啊! [工匠若水 http://blog.csdn.net/yanbob ...
- android的消息处理机制——Looper,Handler,Message
在开始讨论android的消息处理机制前,先来谈谈一些基本相关的术语. 通信的同步(Synchronous):指向客户端发送请求后,必须要在服务端有回应后客户端才继续发送其它的请求,所以这时所有请求将 ...
- Android异步消息处理机制
安卓子线程无法直接更改UI,所以需要异步消息处理机制来解决 <?xml version="1.0" encoding="utf-8"?><Li ...
- android基础(六)android的消息处理机制
Android中的消息处理机制由四个部分组成:Message.Handler.MessageQueue和Looper,并且MessageQueue封装在Looper中,我们一般不直接与MQ打交道. 一 ...
随机推荐
- LESS详解之编译LESS
掌握LESS,必须先掌握LESS的编译.因为LESS是CSS预处理语言的一种,是一种动态语言.LESS可以运行在各种语言和环境中,包括浏览器端.服务器端等.就因为是一种CSS预处理语言,所以需要编 ...
- php之快速入门学习-8(if-else)
PHP If...Else 语句 条件语句用于根据不同条件执行不同动作. PHP 条件语句 当您编写代码时,您常常需要为不同的判断执行不同的动作.您可以在代码中使用条件语句来完成此任务. 在 PHP ...
- android设备上运行i-jetty服务
android设备上运行i-jetty服务: 1) i-jetty安装 本人小菜一个,i-jetty源码有好几个文件,不知道怎么运行起来,于是找了一个现成可运行的i-jetty工程(感谢这位同学的分享 ...
- php如何实现页面跳转
•PHP页面跳转一.header()函数 header()函数是PHP中进行页面跳转的一种十分简单的方法.header()函数的主要功能是将HTTP协议标头(header)输出到浏览器. header ...
- load()和get()的区别
区别1:如果数据库中,没有userId的对象.如果通过get方法加载,则返回的是一个null:如果通过load加载,则返回一个代理对象,如果后面代码如果调用user对象的某个属性(比如user.get ...
- std::move的实际工作过程
std::move的定义如下: template <typename T> typename remove_reference<T>::type && move ...
- 在js里双引号里又加单引号的解决方案常用WdatePicker
EndTime: '<input name="EndTime" type="text" class="editable center decim ...
- 保护HTTP的安全
#如果没有严格的限制访问的权限,公司放在服务器上的重要文档就存在隐患,web需要有一些安全的http形式: #安全方法: #基本认证.摘要认证.报文完整性检查都是一些轻量级的方法,但还不够强大,下面介 ...
- xml 及其语法
先说一些关于框架,开发中的框架,主要是用反射技术来封装数据. 先指定规范写出xml,然后通过读写流技术封装好一些API用来映射xml文件,方便取得xml中的配置信息.取得xml内容就可以通过取得的内容 ...
- HDUOJ---1867 A + B for you again
A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...