码迷,mamicode.com
首页 > 其他好文 > 详细

Handler消息传递机制(一)

时间:2015-01-29 17:42:59      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

MessageQueue:Looper负责管理。采用先进先出的方式管理Message.

Looper:每个线程只有一个Looper,负责管理MessageQueue,会不断从MessageQueue中取出消息,并将消息分给对应的Handler处理

Handler:能把消息发送给Looper管理的MessageQueue,并负责处理Looper分给它的消息(在新启动的线程中发送消息;在主线程中获取、处理消息)

技术分享

声明访问网络权限

<uses-permission android:name="android.permission.INTERNET"/>

package com.example.handler;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private Button button;
	private ImageView imageView;
	private String image_path = 
			"http://pic4.nipic.com/20090828/2366808_092035042961_2.jpg";
	public final int IS_FINISH = 1;
	private ProgressDialog dialog = null;
	private Handler handler = new Handler(){
		
		public void handleMessage(Message msg){
			byte[] data = (byte[]) msg.obj;
			Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
			imageView.setImageBitmap(bm);
			if(msg.what == IS_FINISH){
				dialog.dismiss();
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) findViewById(R.id.button1);
		imageView = (ImageView) findViewById(R.id.imageView1);
		dialog = new ProgressDialog(this);
		dialog.setTitle("提示");
		dialog.setMessage("正在下载,请稍后...");
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new MyThread()).start();
				dialog.show();
			}
		});
	}
	public class MyThread implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(image_path);
			try {
				HttpResponse httpResponse = httpClient.execute(httpGet);
				if(httpResponse.getStatusLine().getStatusCode() == 200){
					byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
					/*
					 * 建议用Message.obtain()而不是new一个Message;会先从消息池中获取
					 * 一个Message以便重复利用,消息池里没有才会new一个Message
					 */
					Message msg = Message.obtain();
					//可传两个整形、一个对象过去,字节数组也是一个对象
					msg.obj = data;
					msg.what = IS_FINISH;
					handler.sendMessage(msg);
				}
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
技术分享技术分享

Handler消息传递机制(一)

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/43272933

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!