android中handler处理message小例子

  大家好!我今天刚申请的博客,也是第一次写博客,希望能和大家交流知识和经验,互相促进学习。可能有不对的地方或者不详细的地方,希望朋友们多多见谅!今天来一起学习一下handler处理Message的简单用法。

  首先,我们了解一下什么是handler:handler主要就是接受子线程发送的message,并用此message配合主线程更新UI 。更新UI只能在主线程中更新,子线程中操作是危险的. 这个时候,Handler就出现了来解决这个复杂的问题,由于Handler运行在主线程中(UI线程中),它与子线程可以通过Message对象来传递数据,这个时候,Handler就承担着接受子线程传过来的(子线程用sedMessage()方法传弟)Message对象,(里面包含数据)  , 把这些消息放入主线程队列中,配合主线程进行更新UI。

  其次就是handler的主要方法:

   post(Runnable)
       postAtTime(Runnable,long)
       postDelayed(Runnable long)
       sendEmptyMessage(int)
       sendMessage(Message)
       sendMessageAtTime(Message,long)
       sendMessageDelayed(Message,long)

  最后我们看一下我写的一个例子:

1.运行界面如下:

2.具体的功能如下:

当你点击自动增加按钮时,textview的数值会每隔一秒自动增加2,当点击暂停按钮时就会停止,当达到规定的最大值(这里我设置的是14)时就停止了,自动增加按钮和暂停按钮失去焦点,不可点击。如下图:

当点击自动减少按钮时,TextView的数值14会自动减少(每秒中减少2),当点击暂停按钮时就会停止,当减少到0的时候,自动减少和暂停按钮就会失去焦点,不可点击。如下图:

3.Acitivity全部代码如下:

package cn.yj3g.handler;

import android.app.Activity;
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.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class HandlerActivity2 extends Activity implements View.OnClickListener {

	private TextView textView;
	private Button increaseButton;
	private Button decreaseButton;
	private Button pauseButton;

	private static final int INCREMENT_PRO = 1;
	private static final int DECREMENT_PRO = 2;
	private static final int PAUSE_PRO = 3;
	private Handler hd = new Handler(){

		@Override
		public void handleMessage(Message msg) {//处理子线程发送过来的消息
			// TODO Auto-generated method stub
			int what = msg.what;
			int i = Integer.parseInt(textView.getText().toString());
			
			switch(what){
			case INCREMENT_PRO:
				i+=2;
				hd.removeMessages(DECREMENT_PRO);
				decreaseButton.setEnabled(true);
				pauseButton.setEnabled(true);
				if(i>14){
					Toast.makeText(HandlerActivity2.this, "已到最大值", 0).show();
					
					textView.setText("14");
					increaseButton.setEnabled(false);
					pauseButton.setEnabled(false);
					return;
				}
				
				textView.setText(i+"");
				hd.sendEmptyMessageDelayed(INCREMENT_PRO, 1000);//发送延迟消息,过一秒 自动增长一次
				break;											//如果没有这句话,数值将不会自动增加
			case DECREMENT_PRO:
				hd.removeMessages(INCREMENT_PRO);
				increaseButton.setEnabled(true);
				pauseButton.setEnabled(true);
				i=i-2;
				if(i<0){
					Toast.makeText(HandlerActivity2.this, "已到最小值", 0).show();
					//hd.removeMessages(DECREMENT_PRO);
					
					textView.setText("0");
					increaseButton.setEnabled(true);
					decreaseButton.setEnabled(false);
					pauseButton.setEnabled(false);
					return;
				}
				
				textView.setText(i+"");
				hd.sendEmptyMessageDelayed(DECREMENT_PRO, 1000);
				break;
			case PAUSE_PRO:
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_handler);
        textView = (TextView)findViewById(R.id.test_text_id);
        
        increaseButton = (Button)findViewById(R.id.test_button1_id);
        decreaseButton = (Button)findViewById(R.id.test_button2_id);
        pauseButton = (Button)findViewById(R.id.test_button3_id);
        increaseButton.setOnClickListener(this);
        decreaseButton.setOnClickListener(this);
        pauseButton.setOnClickListener(this);
    }

	public void onClick(View v) {
         if(v==increaseButton) {
			 Message m = Message.obtain(hd,INCREMENT_PRO);
			 hd.sendMessage(m);
			
		} else if(v==decreaseButton) { 
			Message m = Message.obtain(hd,DECREMENT_PRO);
			hd.sendMessage(m);//发送消息
		} else if(v==pauseButton) {
			hd.removeMessages(INCREMENT_PRO);//去除增长消息
			hd.removeMessages(DECREMENT_PRO);//去除减少消息
		}
	}
}


4.xml文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	
	<TextView android:id="@+id/test_text_id"
		android:layout_marginBottom="10dip"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:gravity="center"
		android:text="0"
/>
	<Button android:id="@+id/test_button1_id"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:layout_marginBottom="10dip"
		android:text="自动增加"/>
		
		
	<Button android:id="@+id/test_button2_id"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:text="自动减少"
		android:layout_marginBottom="10dip"/>
	<Button android:id="@+id/test_button3_id"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:text="暂停" />
</LinearLayout>
希望对大家有帮助。
原文地址:https://www.cnblogs.com/limingblogs/p/2198731.html