Handler、Message的简单使用

Handler是用来发送和处理Message及Runnable对象的,它与线程的消息队列关联。每个Handler实例都绑定到创建它的线程及其消息队列。Looper是用于运行线程消息循环的类,而MessageQueue则存储待处理的消息。通过Looper.loop()方法可以启动消息循环,直到调用Looper.quit()来结束。
摘要由CSDN通过智能技术生成
Android没有全局的消息队列,Android的消息队列是和某个线程相关联在一起的。每个线程最多只有一个消息队列,消息的处理也是在这个线程中完成。也就是说,如果想在当前线程中使用消息模型,则必须构建一个消息队列,消息机制的主要类是:Looper、Handler、MessageQueue、Message.
1、
public class

Handler

extends Object
java.lang.Object
   ↳ android.os.Handler

Class Overview

A Handler allows you to send and process Message and Runnable objects associated with a thread'sMessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.
 

 mHandler = new Handler(){
	public void handleMessage(Message msg){
//int android.os.Message.what
//User-defined message code so that the recipient can identify what this message is about.
switch(msg.what){
		 case UPDATE_TEXT:
		mTextView.setText("text changed");
		 break;
		default:
		 break;
		}
	                                }
		};


public final boolean sendMessage(Message msg)Added inAPI level 1

Pushes a message onto the end of the message queue after all pending messages before the current time. It will be received inhandleMessage(Message), in the thread attached to this handler.

Returns
  • Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
Message mMessage = Message.obtain(mHandler, UPDATE_TEXT);

//Pushes a message onto the end of the message queue after all pending messages before the current time. 
mHandler.sendMessage(mMessage);
Handler负责将Message发送至当前线程的MessageQueue中,处理消息。发送消息一般是使用Handler的sendMessage方法,发出的消息最终会传递到Handler的handleMessage()方法中。
public final class

2、

Looper

extends Object
java.lang.Object
   ↳ android.os.Looper

Class Overview

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, callprepare() in the thread that is to run the loop, and thenloop() to have it process messages until the loop is stopped.

Most interaction with a message loop is through the Handler class. 

Looper时时刻刻监视着MessageQueue, 是每个线程中的MessageQueue管家,每个线程中只有一个Looper,调用其loop()方法就会进入到一个无限循环中,每当发现MessageQueue中存在一条消息,就
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MessageHandler是Android中的一个接口,用于处理来自其他组件或线程的消息。在开发一个App时,你可以使用MessageHandler来处理和响应来自用户的消息、来自服务的消息、或者来自其他应用程序的消息。 以下是一些使用MessageHandler的步骤: 1.创建一个类来扩展MessageHandler,并实现handleMessage()方法。 2.在你的App中,创建一个Handler对象,并传入你刚才创建的MessageHandler类的实例。 3.在你的App中,使用Handler对象向MessageQueue发送消息。 4.当消息发送到MessageQueue中时,MessageQueue会将消息发送到MessageHandler中。 5.在MessageHandler中,你可以根据不同的消息类型,执行不同的操作。 下面是一个简单的示例代码: ``` public class MyMessageHandler extends MessageHandler { @Override public void handleMessage(Message msg) { // 根据消息类型,执行不同的操作 switch (msg.what) { case 1: // 处理消息类型为1的操作 break; case 2: // 处理消息类型为2的操作 break; default: // 处理其他类型的操作 break; } } } // 在Activity中创建Handler对象,并传入MyMessageHandler类的实例 Handler handler = new Handler(new MyMessageHandler()); // 发送消息到MessageQueue中 Message msg = Message.obtain(); msg.what = 1; handler.sendMessage(msg); ``` 在上面的代码中,我们创建了一个名为MyMessageHandler的类,它继承自MessageHandler,重写了handleMessage()方法。在Activity中,我们创建了一个Handler对象,并将MyMessageHandler类的实例传入。然后我们发送了一个消息到MessageQueue中,消息类型为1。当消息被发送到MessageQueue中时,MessageQueue会将消息发送到MyMessageHandler中,并执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值