Hander , HandlerThread 用法

package com.example.handlerdemo2;

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

public class MainActivity extends Activity {

	private Button startButton = null;
	private ProgressBar progressBar = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		startButton = (Button)findViewById(R.id.start_button);
		progressBar = (ProgressBar)findViewById(R.id.progress_bar);
		startButton.setOnClickListener(new StartButtonListener());
	}

	@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;
	}
	
	class StartButtonListener implements OnClickListener
	{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			// Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached.
			// 这里把线程加入消息队列, 这里离线程是主线程, 

			handler.post(updateThread);
			
		}
		
	}
	
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			System.out.println("handleMessage");
			if(msg.arg1 > 100)
			{
				handler.removeCallbacks(updateThread);
			}
			else
			{
				progressBar.setProgress(msg.arg1);
				handler.post(updateThread);
			}
		}
		
	};
	Runnable updateThread = new Runnable(){

		int i = 0;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			System.out.println("Begin thread");
			i += 10;
			Message msg = handler.obtainMessage();
			msg.arg1 = i;
			try{
				Thread.sleep(1000);
			}catch(InterruptedException ex){
				ex.printStackTrace();
			}
			//Pushes a message onto the end of the message queue after all pending messages before the current time. 
			//It will be received in handleMessage(Message), in the thread attached to this handler.

			handler.sendMessage(msg);
			//视频是在这里做半段, 在这里做判断会导致死循环,因为 removeCallbacks 把线程移出消息队列,并没有删除, 
			//而 当调用sendMessage(msg) 后会调用 handlerMessage()这个方法,又把线程加入消息队列
//			if(i >= 100)
//			{
//				System.out.println("removeCallbacks");
//				handler.removeCallbacks(updateThread);
//			}
		}
		
	};
	
}

下面是 HandlerThread  ,开辟新线程

package com.example.handlerthreaddemo2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//生成 HandlerThread对象
		HandlerThread handlerThread = new HandlerThread("handler_thread"); 
		// 在 调用handlerThread.getLooper() 之前一定要  start() ,否则调用getLooper()取得空
		handlerThread.start();
		MyHandler handler = new MyHandler(handlerThread.getLooper());
		Message msg = handler.obtainMessage();
		//Bundle 用来传递数据, 可以简单看成特别的map , key 只能是String , 
		Bundle data = new Bundle();
		data.putInt("age", 11);
		data.putString("name", "zhangsan");
		msg.setData(data);
		msg.sendToTarget();
		
	}

	@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;
	}

	class MyHandler extends Handler{
		public MyHandler()
		{
			
		}
		
		public MyHandler(Looper looper)
		{
			super(looper);
		}
		
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			Bundle bundle = msg.getData();
			System.out.println("name:" + bundle.get("name") + "age:" + bundle.get("age"));
			System.out.println("myHandler" + Thread.currentThread().getId());
			//这里打印  名字  是   handler_thread 开始构造函数输入的名称
			System.out.println("myHandler" + Thread.currentThread().getName());
		}
		
		
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值