博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Looper
阅读量:6555 次
发布时间:2019-06-24

本文共 3333 字,大约阅读时间需要 11 分钟。

/**   * Class used to run a message loop for a thread.  Threads by default do   * not have a message loop associated with them; to create one, call   * {@link #prepare} in the thread that is to run the loop, and then   * {@link #loop} to have it process messages until the loop is stopped.   *   * 

Most interaction with a message loop is through the * {@link Handler} class. * *

This is a typical example of the implementation of a Looper thread, * using the separation of {@link #prepare} and {@link #loop} to create an * initial Handler to communicate with the Looper. * *

   *  class LooperThread extends Thread {
* public Handler mHandler; * * public void run() {
* Looper.prepare(); * * mHandler = new Handler() {
* public void handleMessage(Message msg) {
* // process incoming messages here * } * }; * * Looper.loop(); * } * }
*/
/** Initialize the current thread as a looper.   * This gives you a chance to create handlers that then reference   * this looper, before actually starting the loop. Be sure to call   * {@link #loop()} after calling this method, and end it by calling   * {@link #quit()}.   */ public static void prepare() {
prepare(true); } private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); }
/**  * Run the message queue in this thread. Be sure to call  * {@link #quit()} to end the loop.  */ public static void loop() {
final Looper me = myLooper(); if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); } final MessageQueue queue = me.mQueue; // Make sure the identity of this thread is that of the local process, // and keep track of what that identity token actually is. Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity(); for (;;) {
Message msg = queue.next(); // might block if (msg == null) {
// No message indicates that the message queue is quitting. return; } // This must be in a local variable, in case a UI event sets the logger Printer logging = me.mLogging; if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } msg.target.dispatchMessage(msg); if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); } // Make sure that during the course of dispatching the // identity of the thread wasn't corrupted. final long newIdent = Binder.clearCallingIdentity(); if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x" + Long.toHexString(ident) + " to 0x" + Long.toHexString(newIdent) + " while dispatching to " + msg.target.getClass().getName() + " " + msg.callback + " what=" + msg.what); } msg.recycleUnchecked(); } }

转载地址:http://tknco.baihongyu.com/

你可能感兴趣的文章
UIKeyboard键盘相关知识点-IOS开发
查看>>
你真的会 snapshot 吗? - 每天5分钟玩转 OpenStack(163)
查看>>
onAttachedToWindow和onDetachedFromWindow调用时机源码解析
查看>>
虚拟机外接USB设备情况的vMotion问题
查看>>
Mysql数据库大小查询
查看>>
#78 Reimplement Trampoline
查看>>
使用Java制作图文验证码
查看>>
java学习笔记----之多线程开发
查看>>
使用javap分析return和finally的执行字节码
查看>>
java 代理
查看>>
数据库设计三范式
查看>>
Eclipse插件开发- view to view drag drop
查看>>
Linux 技巧:让进程在后台可靠运行的几种方法
查看>>
ORACLE特殊字符的处理方法
查看>>
根据Servlet的Filter自定义实现字符编码过滤器
查看>>
shiro之Remembered vs. Authenticated
查看>>
碉堡了!又一只会跑酷的狗狗!
查看>>
python入门(一)-- 简介与基本语法
查看>>
oh-my-zsh安装与配置
查看>>
pyramid学习笔记整理
查看>>