android开发handler与线程的用法

在android的开发中,如果用Handler来启动Runable函数的run,这都知在主线程中运行,而不是新开一个线程单独运行,这样如果run中运行没有结果就会影响主线程中的Activity,导致应用无响应。所以android就有一个类“HandlerThread”,通过它来新建一个子线程与主线程分开,再把这样子线程绑定在新建的Handler对象上,这样run函数就会在子线程中运行,而不影响Activity。

 

        下面例子是线程来控制一个水平进度条的例子:

 

package com.example.taxtbarhandler01;

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

public class Taxtnarhandler01 extends Activity {

	private Button start;
	private Button end;
	//进度条
	private ProgressBar bar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		start = (Button)findViewById(R.id.start);
		end = (Button)findViewById(R.id.end);
		bar = (ProgressBar)findViewById(R.id.progressBar1);
		bar.setMax(100);
		//输出当前线程的名字与ID;
		System.out.println("Activity-->" + Thread.currentThread().getId());
		System.out.println("Activity-->" + Thread.currentThread().getName());
		//start监听器
		start.setOnClickListener(new Startlistener());
		end.setOnClickListener(new Endlistener());
	}

	Myhandler handler ;
	class Startlistener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			//输出当前线程的名字与ID;
			System.out.println("start-->" + Thread.currentThread().getId());
			System.out.println("start-->" + Thread.currentThread().getName());
			bar.setVisibility(View.VISIBLE);
			//创建一个子线程;
			HandlerThread updata = new HandlerThread("name");
			updata.start();
			//把子线程绑定在handler中
			handler = new Myhandler(updata.getLooper());
			handler.post(r);
		}
		
	}
	
	class Endlistener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//输出当前线程的名字与ID;
			System.out.println("end-->" + Thread.currentThread().getId());
			System.out.println("end-->" + Thread.currentThread().getName());
			bar.setVisibility(View.GONE);
		}
		
	}
	
	
	//匿名内部类
	class Myhandler extends Handler{
		public Myhandler(){
			
		}
		public Myhandler(Looper looper){
			super(looper);
		}
		@Override
		//从消息队列中取出消息处理
		public void handleMessage(Message msg) {
			//输出当前线程的名字与ID;
			System.out.println("handler-->" + Thread.currentThread().getId());
			System.out.println("handler-->" + Thread.currentThread().getName());
			// TODO Auto-generated method stub
			//更新进度条
			bar.setProgress(msg.arg1);
			//进入运行队列
			handler.post(r);
			if (msg.arg1 == 100)
			{
				handler.removeCallbacks(r);
				//注意,因为进度条bar是在主线程中建立的,而这个函数是在子线程中运行,所以不能在这里改变bar的状态,否则会报错(这是我个人理解,因为报错了-_-!)
		//		bar.setVisibility(View.GONE);
			}
		}
		
	}
	
	Runnable r = new Runnable() {
		int i = 0;
		@Override
		public void run() {
			//输出当前线程的名字与ID;
			System.out.println("run-->" + Thread.currentThread().getId());
			System.out.println("run-->" + Thread.currentThread().getName());
			// TODO Auto-generated method stub
			System.out.println("++" + i);
			i = i + 10;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//传送消息
			Message msg = handler.obtainMessage();
			msg.arg1 = i;
			//消息入队列
			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;
	}

}

你可能感兴趣的:(android)