Thread+Handler的简单例子

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">现在我比较喜欢用Thread+Handler,暂时觉得还挺方便的,这个例子只是简单的网路下载一张图片,然后展示而已</span>

自定义一个Thread的子类:

主要的作用是去网络获取数据:

private class DownloadImageThread extends Thread {
		private String url_path;

		public DownloadImageThread(String url_path) {
			this.url_path = url_path;
		}

		@Override
		public void run() {
			super.run();
			HttpURLConnection conn = null;
			InputStream is = null;
			try {
				URL url = new URL(url_path);
				conn = (HttpURLConnection) url.openConnection();
				is = conn.getInputStream();

				Bitmap bmp = BitmapFactory.decodeStream(is);
				/** 封装消息 */
				Message msg = handler.obtainMessage();
				msg.what = 1;
				msg.obj = bmp;
				/** 发送消息 */
				handler.sendMessage(msg);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if (is != null) {
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (conn != null) {
					conn.disconnect();
				}
			}
		}
	}

Handler的子类:

public class ImageHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			/**判断消息的不同来处理不同消息*/
			switch (msg.what) {
			case 1:
				Bitmap bmp = (Bitmap) msg.obj;
				iv.setImageBitmap(bmp);
				break;
			}
		}
	}
 

平时都会出错,经常因为快捷键,所以常把那个HttpUrlConnection类给写成HttpsURLConnection

这个要注意了:

整体的代码很简单(老是忘记加权限了):

public class MainActivity extends Activity {
	private ImageView iv;
	private ImageHandler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initUI();
		handler = new ImageHandler();
		new DownloadImageThread(
				"http://f.hiphotos.baidu.com/image/w%3D1280%3Bcrop%3D0%2C0%2C1280%2C720/sign=8aa1bdb5af345982c58ae19034c40ace/6c224f4a20a44623148b8ca39a22720e0df3d7a9.jpg")
				.start();

	}

	private void initUI() {
		iv = (ImageView) findViewById(R.id.iv);
	}

	private class DownloadImageThread extends Thread {
		private String url_path;

		public DownloadImageThread(String url_path) {
			this.url_path = url_path;
		}

		@Override
		public void run() {
			super.run();
			HttpURLConnection conn = null;
			InputStream is = null;
			try {
				URL url = new URL(url_path);
				conn = (HttpURLConnection) url.openConnection();
				is = conn.getInputStream();

				Bitmap bmp = BitmapFactory.decodeStream(is);
				/** 封装消息 */
				Message msg = handler.obtainMessage();
				msg.what = 1;
				msg.obj = bmp;
				/** 发送消息 */
				handler.sendMessage(msg);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if (is != null) {
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (conn != null) {
					conn.disconnect();
				}
			}
		}
	}

	public class ImageHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			/**判断消息的不同来处理不同消息*/
			switch (msg.what) {
			case 1:
				Bitmap bmp = (Bitmap) msg.obj;
				iv.setImageBitmap(bmp);
				break;
			}
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值