Android Looper Handler

1.首先我们来了解一下负责管理线程消息队列和消息循环的Looper

 

Looper 类应用于在线程中建立一个消息循环,线程默认是没有消息循环的,我们可以通过在线程中调用 prepare() 创建消息循环,然后通过loop() 进入消息循环。

大多数消息循环是通过 Handler 类来实现的。

下面是一个简单的例子:


 

 

Public Methods

public void  dump   (Printer   pw,  String   prefix)
Since:   API Level 1
public static final synchronized  Looper   getMainLooper   ()
Since:   API Level 1

返回程序的主消息循环,也就是在程序的主线程中生存的消息循环

Returns the application's main looper, which lives in the main thread of the application.

public  Thread   getThread   ()
Since:   API Level 3

返回和这个消息循环相关联的线程

Return the Thread associated with this Looper.

public static final void  loop   ()
Since:   API Level 1

在线程中运行消息队列

Run the message queue in this thread. Be sure to call  quit()   to end the loop.

public static final  Looper   myLooper   ()
Since:   API Level 1

返回和当前线程相关联的消息队列

Return the Looper object associated with the current thread. Returns null if the calling thread is not associated with a Looper.

public static final  MessageQueue   myQueue   ()
Since:   API Level 1

返回和当前线程相关联的消息队列,这个方法必须在一个正在执行这个消息循环的线程中运行

Return the  MessageQueue   object associated with the current thread. This must be called from a thread running a Looper, or a NullPointerException will be thrown.

public static final void  prepare   ()
Since:   API Level 1

初始化当前线程为一个消息循环

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 callloop()   after calling this method, and end it by calling  quit() .

public static final void  prepareMainLooper   ()
Since:   API Level 1

初始化一个主消息循环

Initialize the current thread as a looper, marking it as an application's main looper. The main looper for your application is created by the Android environment, so you should never need to call this function yourself.  prepare()

public void  quit   ()
Since:   API Level 1
public void  setMessageLogging   (Printer   printer)
Since:   API Level 1

Control logging of messages as they are processed by this Looper. If enabled, a log message will be written to  printer   at the beginning and ending of each message dispatch, identifying the target Handler and message contents.

Parameters
printerA Printer object that will receive log messages, or null to disable message logging.
public  String   toString   ()
Since:   API Level 1

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation simply concatenates the class name, the '@' sign and a hexadecimal representation of the object's  hashCode() , that is, it is equivalent to the following expression:

 getClass
().
getName
()
 
+
 
'@'
 
+
 
Integer
.
toHexString
(
hashCode
())

 
Returns
  • a printable representation of this object.

2.Handler
Handler的用途主要有2个方面:
(1)安排计划一些在程序运行过程中将要处理的消息和任务
(2)把这一消息队列在其他线程中处理
to be continued .......



 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值