//運用Handler.post() public class SecondActivity extends Activity { private String url = "http://litchiapi.jstv.com/Attachs/Top/11949/e650e0201de541d2ba91dca202b0fcfe.jpg"; private ImageView imageview; private Handler handler = new Handler();// 注意:這裏用handler.post()方法,因此不用實現裏面的方法 // 這種方法是不用到Message(),只是利用handler.post(Runable)方法來更新,裏面其實放的是代碼,注意:存放的代碼它屬於主線程; // 注意:這裏用handler.post()方法,因此不用實現裏面的方法 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 這個SecondActivity要用主佈局記得在功能清單文件改配置 imageview = (ImageView) this.findViewById(R.id.imageview); // 子線程,下載操做 new Thread(new Runnable() { @Override public void run() { Log.i("MainActivity", "first" + Thread.currentThread().getName()); byte[] data = loadImage(url); final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); handler.post(new Runnable() { // 注意:這裏是主線程,只是一個代碼,由於它沒有啓動因此不算線程 @Override public void run() { Log.i("MainActivity", "second" + Thread.currentThread().getName()); imageview.setImageBitmap(bitmap); } }); } }).start(); } public byte[] loadImage(String url) { try { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toByteArray(response.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } } //使用handler.sendEmptyMessage()通知更新 //這是第一種方法:利用handler.sendEmptyMessage()通知更新 //另一種方法是不用到Message(),只是利用handler.post(Runable)方法來更新,裏面其實放的是代碼,注意:存放的代碼它屬於主線程; public class MainActivity extends Activity { private String url = "http://litchiapi.jstv.com/Attachs/Top/11949/e650e0201de541d2ba91dca202b0fcfe.jpg"; private ImageView imageview; private ProgressDialog dialog; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: dialog.show(); break; case 1: imageview.setImageBitmap((Bitmap) msg.obj); break; case 2: dialog.dismiss(); break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview = (ImageView) this.findViewById(R.id.imageview); dialog = new ProgressDialog(this); dialog.setTitle("提示"); dialog.setIcon(R.drawable.ic_launcher); dialog.setMessage("正在下載數據!!!"); new Thread(new Runnable() { @Override public void run() { // 1.顯示對話框 handler.sendEmptyMessage(0);// 通知主線程顯示對話框 // 2.下載數據 byte[] data = loadImage(url); Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); Message message = Message.obtain(); message.what = 1; message.obj = bitmap; handler.sendMessage(message);// 發送主線程設置圖片顯示 // 3.關閉對話框 handler.sendEmptyMessage(2); } }).start(); } public byte[] loadImage(String url) { try { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toByteArray(response.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }