DO onething is everything 之android--handler总结(一)

前言:看了mars老师的android的视频讲handler以及结合网上的资料,自己总结下handler的用法。

一、什么是Handler:

自己理解:官方文档的解释是A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. 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 creatinit -- 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.

一个handler允许你发送并加工信息和与线程的消息队列紧密相连的Runnable对象。
其实就是用来管理线程的,而我们知道java当中有两种方法来实现多线程,一个是继承Thread类,另外一种是实现Runnable接口。

网上解释:(比我理解的专业多了,因此就摘抄了下来,冒犯请原谅):


Handler 为Android操作系统中的线程通信工具,它主要由两个作用:
(1)安排消息或Runnable 在某个主线程中某个地方执行(2)安排一个动作在另外的线程中执行。每个Handler对象维护两个队列


(FIFO),消息队列和Runnable队列, 都是有Android操作系统提供的。Handler可以通过这两个队列来分别:


发送、接受、处理消息–消息队列;
启动、结束、休眠线程–Runnable队列;

二 使用handler:


Handler的使用方法大体分为3个步骤:1.创建Handler对象。2.创建Runnable和消息。3.调用post以及sendMessage方法将


Runnable和消息添加到队列。

1、创建handler对象:

Handler handler=new Handler();

2、建立线程

Thread updateThread = new Thread()或
Runnable updateThread = new Runnable()

3分发消息方法:

post(Runnable):将Runnable直接添加入队列
postAtTime(Runnable,long):定时Runnable添加入队列
postDelayed(Runnable,long):延迟一定时间后,将Runnable添加入队列
sendEmptyMessage(int):将空消息添加入消息队列
sendMessage(Message):将Message添加到消息队列
sendMessageAtTime(Message,long):定时将message添加到消息队列
sendMessageDelayed(Message,long):延迟一定时间后,将Message添加入队列

handler.removeCallbacks(thread);将Runnable从Runnable队列中取出


下面是一段简单的代码示例:

package com.dengchao.handlertest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	
	
	private Button startButton;
	private Button endButton;
	

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //根据控件id得到代表控件的对象,并为这两个控件添加监听器
        startButton = (Button)findViewById(R.id.startButton);
        startButton.setOnClickListener(new StartButtonListener());
        
        endButton =(Button)findViewById(R.id.endButton);
        endButton.setOnClickListener(new EndButtonListener());
        
    }


   
    class StartButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			//这个方法的作用是当执行这个对象的是时候马上加入到现成队列中去,
			//而这个updateThread对象被加入到队列之后立即执行run方法
			
		handler.post(updateThread);
			
		}


		
    	
    }
    
    
    class EndButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//把updateThread移出线程队列
			handler.removeCallbacks(updateThread);
		}
    	
    	
    } 
    
    //新建一个handler对象
    Handler handler =new Handler();
    //利用新建匿名内部类的方法来调用runnable接口
    Runnable updateThread = new Runnable() {
 
		
		@Override
		//重写run方法
		public void run() {
			// TODO Auto-generated method stub
			//
			
			System.out.println("UpdateThread");
			//延迟3000pm后把updateThread放入消息队列
			handler.postDelayed(updateThread,3000);
			
			
			
		}
	};
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

这个代码实现的是当点击StartButton的时候,在logCat中打印“UpdateThread”,并且每隔3秒钟就会打印一次,而当点击Endbutton的时候就停止打印。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值