安卓Handler和Message(一)

activity_main.xml



    


MainActivity

package com.example.android_asytask_download;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Entity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private Button button;
	private ImageView imageView;
	private ProgressDialog progressDialog;
	private final int IS_END = 1;
	private String image_path = "http://a.hiphotos.baidu.com/image/w%3D2048/sign=7610c70c5143fbf2c52ca1238446cb80/d4628535e5dde71120a3a1f2a5efce1b9d166145.jpg";
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			byte[] data = (byte[]) msg.obj;
			Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
			imageView.setImageBitmap(bitmap);
			if(msg.what==IS_END)
			{
				progressDialog.dismiss();
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		button = (Button) this.findViewById(R.id.button1);
		imageView = (ImageView) this.findViewById(R.id.imageView1);
		progressDialog = new ProgressDialog(this);
		progressDialog.setTitle("提示");
		progressDialog.setMessage("正在下载,请稍后...");
		progressDialog.setCancelable(false);// 点击不会失去焦点,直到下载结束

		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new Thread(new MyThread()).start();
				progressDialog.show();
			}
		});
	}

	public class MyThread implements Runnable {
		@Override
		public void run() {
			// 线程耗时部分的主体操作
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(image_path);
			try {
				HttpResponse httpResponse = httpClient.execute(httpGet);// 执行http操作
				if (httpResponse.getStatusLine().getStatusCode() == 200) {
					byte[] data = EntityUtils.toByteArray(httpResponse
							.getEntity());
					// 一般实例化消息对象不用new Message(),而是通过Message.obtain()
					Message message = Message.obtain();
					message.obj = data;
					message.what = IS_END;
					handler.sendMessage(message);// handler对象发送消息
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

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

}


安卓Handler和Message(一)_第1张图片安卓Handler和Message(一)_第2张图片安卓Handler和Message(一)_第3张图片

1.Handler对象负责发送消息和处理消息

2.Message消息对象实例化使用Message message = Message.obtain();

你可能感兴趣的:(安卓多线程)