设为首页 加入收藏

TOP

Android Handler Message多线程实例(一)
2014-11-24 11:27:58 来源: 作者: 【 】 浏览:0
Tags:Android Handler Message 线程 实例

什么是Handler


handler通俗讲就是在各个进程之间发送数据的处理对象。在任何进程中,只要获得了另一个进程的handler则可以通过 handler.sendMessage(message)方法向那个进程发送数据。基于这个机制,我们在处理多线程的时候可以新建一个thread,这 个thread拥有UI线程中的一个handler。当thread处理完一些耗时的操作后通过传递过来的handler向UI线程发送数据,由UI线程去更新界面。


Handler类简介


Handler类的常用方法



开发带有Handler类的程序步骤如下。


在Activity或Activity的Widget中开发Handler类的对象,并重写handleMessage方法。


在新启动的线程中调用sendEmptyMessage或者sendMessage方法向Handler发送消息。


Handler类的对象用handleMessage方法接收消息,然后根据消息的不同执行不同的操作。


在Android 中Handler和Message、Thread有着很密切的关系。Handler 主要是负责Message的分发和处理。但是这个Message从哪里来的呢?Message 由一个消息队列进行管理,而消息队列却由一个Looper进行管理。Android系统中Looper负责管理线程的消息队列和消息循环,具体实现请参考Looper的源码。 可以通过Loop.myLooper()得到当前线程的Looper对象,通过Loop.getMainLooper()可以获得当前进程的主线程的 Looper对象。Android系统的消息队列和消息循环都是针对具体线程的,一个线程可以存在(当然也可以不存在)一个消息队列和一个消 息循环(Looper),特定线程的消息只能分发给本线程,不能进行跨线程,跨进程通讯。但是创建的工作线程默认是没有消息循环和消息队列的,如果想让该 线程具有消息队列和消息循环,需要在线程中首先调用Looper.prepare()来创建消息队列,然后调用Looper.loop()进入消息循环。


虽说 特定线程的消息只能分发给本线程,不能进行跨线程通讯,但是由于可以通过获得线程的Looper对象来进行曲线的实现不同线程间消息的传递,代码如下


package com.mytest.handlertest;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class HandlerTest extends Activity implements OnClickListener{

private String TAG = "HandlerTest";
private boolean bpostRunnable = false;

private NoLooperThread noLooperThread = null;
private OwnLooperThread ownLooperThread = null;
private ReceiveMessageThread receiveMessageThread =null;

private Handler mOtherThreadHandler=null;
private EventHandler mHandler = null;

private Button btn1 = null;
private Button btn2 = null;
private Button btn3 = null;
private Button btn4 = null;
private Button btn5 = null;
private Button btn6 = null;
private TextView tv = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(250, 50);
layout.setOrientation(LinearLayout.VERTICAL);

btn1 = new Button(this);
btn1.setId(101);
btn1.setText("message from main thread self");
btn1.setOnClickListener(this);
layout.addView(btn1, params);

btn2 = new Button(this);
btn2.setId(102);
btn2.setText("message from other thread to main thread");
btn2.setOnClickListener(this);
layout.addView(btn2,params);

btn3 = new Button(this);
btn3.setId(103);
btn3.setText("message to other thread from itself");
btn3.setOnClickListener(this);
layout.addView(btn3, params);

btn4 = new Button(this);
btn4.setId(104);
btn4.setText("message with Runnable as callback from other thread to main th

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java创建线程的三种方式 下一篇如何使用Java synchronized进行线..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C 内存管理 | 菜鸟教 (2025-12-26 20:20:37)
·如何在 C 语言函数中 (2025-12-26 20:20:34)
·国际音标 [ç] (2025-12-26 20:20:31)
·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)