關於Android多線程處理UI-我在網上查了下資料發現有好幾種,本次學習只是其中一種,主要是利用handler結合Thread更新UI。
下面是我寫的小Demo:
Handler+thread 加載網絡圖片,我在網上隨便找了三張圖片,分別開三個線程加載他們,然後在Activity中顯示。
其中一個線程出現異常不會影響到其他線程更不會阻塞主線程(UI線程) ,這是多線程帶來的好處之一。
本次Demo主要是利用Handler.sendMessage(...)把消息壓進消息隊列,過後通過Handler.handleMessage(...)在UI線程中處理壓入的消息。從隊列中取出消息時會根據壓入的不同消息來更新UI。圖片在外部線程中加載,加載完後sendmessage在主線程中更新UI。外部線程只顧加載圖片,而更新UI是主線程(UI線程)的事,這個就達到了多線程異步加載網絡圖片的目的
ThreadDemo.java ThreadDemo是一個Activity,UI在這裡面更新
- package com.study.thread;
-
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
- import android.widget.Toast;
-
- public class ThreadDemo extends Activity {
- private static final String TAG = "ThreadDemo";
- // 我把三個線程編了號
- private static final int THREAD_1 = 1;
- private static final int THREAD_2 = 2;
- private static final int THREAD_3 = 3;
- private ImageView mImgView1;
- private ImageView mImgView2;
- private ImageView mImgView3;
-
- // 記錄消息
- int count = 0;
-
- ProgressDialog mDialog ;
-
- // 網上找的三張圖片的地址
- String[] urls = new String[]
- {
- "http://hiphotos.baidu.com/yuangengqiang/pic/item/252196ca222b88ad50664f58.jpg",
- "http://hiphotos.baidu.com/lzc196806/pic/item/d84f738da76514d2f11f3665.jpg",
- "http://hiphotos.baidu.com/735216726/pic/item/2488f146f7ea415e72f05d75.jpg"
- };
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.getView();
- System.out.println("ThreadDemo-----"+Thread.currentThread().getName());
-
- // 處理按鈕按下
- findViewById(R.id.btnStart).setOnClickListener(new OnClickListener()
- {
-
- @Override
- public void onClick(View v)
- {
- count = 0;
- mDialog = ProgressDialog.show(ThreadDemo.this, "",
- "Loading. Please wait...", true);
- // 開啟線程:第一個線程處理第一張圖片以此類推
- new Thread(new LoadImageRunnable(mHandler,THREAD_1,urls[0])).start();
- new Thread(new LoadImageRunnable(mHandler,THREAD_2,urls[1])).start();
- new Thread(new LoadImageRunnable(mHandler,THREAD_3,urls[2])).start();
- ThreadDemo.this.setTitle("線程已近創建完畢");
- }
-
- });
- }
-
- private void getView()
- {
- mImgView1 = (ImageView)findViewById(R.id.img01);
- mImgView2 = (ImageView)findViewById(R.id.img02);
- mImgView3 = (ImageView)findViewById(R.id.img03);
- }
-
-
-
- private Handler mHandler = new Handler()
- {
-
- // 利用handleMessage更新UI
- public void handleMessage (Message msg)
- {
- switch(msg.what)
- {
- case ThreadDemo.THREAD_1:
- mImgView1.setImageBitmap((Bitmap)msg.obj);
- Log.i(TAG, "thread-1");
- break;
- case ThreadDemo.THREAD_2:
- mImgView2.setImageBitmap((Bitmap)msg.obj);
- Log.i(TAG, "thread-2");
- break;
- case ThreadDemo.THREAD_3:
- mImgView3.setImageBitmap((Bitmap)msg.obj);
- Log.i(TAG, "thread-3");
- break;
-
- // 如有異常會有提示
- default:
- String info = "第"+msg.what%10+"個線程"+"出現異常";
- Toast.makeText(ThreadDemo.this, info , Toast.LENGTH_LONG).show();
- break;
-
- }
- count++;
- if(3==count)
- {
- mDialog.dismiss();
- }
-
- }
- };
- }