idleHandler
Posted 渡口一艘船
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了idleHandler相关的知识,希望对你有一定的参考价值。
idleHandler
什么是idleHandler
android是基于Looper消息循环的系统,我们通过Handler向Looper包含的MessageQueue投递Message,在MessageQueue中我们可以看到这样的一个接口
/** * Callback interface for discovering when a thread is going to block * waiting for more messages. */ public static interface IdleHandler /** * Called when the message queue has run out of messages and will now * wait for more. Return true to keep your idle handler active, false * to have it removed. This may be called if there are still messages * pending in the queue, but they are all scheduled to be dispatched * after the current time. */ boolean queueIdle();
从信息描述可以看到在Looper里面的Messsage暂时处理完了,这个时候会回调这个接口,即 ”idle“,
返回值
如果返回false用完就会移除这个接口,相当于使用一次,后面这个消息就从队列移除了
返回true就会保留,在下次Looper空闲时继续处理。比如在主线程return true,就会出现一直轮询
作用
从上面可以看出,这个仅仅是在消息队列暂时处理完会调用这个回调,那么就有下面两种场景可以使用
UI线程 即主线程
用法
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() @Override public boolean queueIdle() /*dosometing*/ return false; );
actvity中UI绘制完全结束时机
onStart是用户可见,onResume是用户可交互,此时绘制不一定完成了,而idler等到这些都完成了,再去执行自己,因此 我们获取一些宽高可以在这里做。
提升性能
在做性能优化时候,可以错开执行消息队列,将一些耗时操作放到这里执行,比如我们国际化司机端,首页有个Google地图,还有一些出车文字滚动和波纹动画之类的,此时就可以将出车动画放在idleHanlder中,让底图先渲染。
自定义Looper 如HandlerThread
除了主线程之外,我们也可以自定义自己的Looper去处理,这时候可以让自己Looper关联上idlerHandler在Looper消息处理完时再去处理其他的东西,
MessageQueue messageQueue = handlerThread.getLooper().getQueue(); messageQueue.addIdleHandler(new MessageQueue.IdleHandler() @Override public boolean queueIdle() return true; );
比如在单线程异步场景中,多次快速数据,但是只需要显示一次,就可以使用这种方式
注意点
要是有强时序性的操作最好不好放到这里处理,容易出问题
参考
- https://wetest.qq.com/lab/view/352.html
- https://www.jianshu.com/p/adab1564d16b
- https://www.jianshu.com/p/a1d945c4f5a6
- https://www.wanandroid.com/wenda/show/8723
以上是关于idleHandler的主要内容,如果未能解决你的问题,请参考以下文章