Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android的handler異步更新實例

Android的handler異步更新實例

編輯:Android開發實例

 方法一:

  1. package com.example.handlerdemo; 
  2.  
  3. import org.apache.http.HttpResponse; 
  4. import org.apache.http.client.HttpClient; 
  5. import org.apache.http.client.methods.HttpGet; 
  6. import org.apache.http.impl.client.DefaultHttpClient; 
  7.  
  8. import android.app.Activity; 
  9. import android.graphics.Bitmap; 
  10. import android.graphics.BitmapFactory; 
  11. import android.os.Bundle; 
  12. import android.os.Handler; 
  13. import android.os.Message; 
  14. import android.view.View; 
  15. import android.widget.ImageView; 
  16. import android.widget.Toast; 
  17.  
  18. public class MainActivity extends Activity { 
  19.     private static final int MSG_SUCCESS = 0;// 獲取圖片成功的標識 
  20.     private static final int MSG_FAILURE = 1;// 獲取圖片失敗的標識 
  21.     ImageView mImageView; 
  22.     Handler mHandler = new Handler() { 
  23.         public void handleMessage(Message msg) { 
  24.             switch (msg.what) { 
  25.             case MSG_SUCCESS: 
  26.                 mImageView.setImageBitmap((Bitmap) msg.obj);// imageview顯示從網絡獲取到的logo 
  27.                 break; 
  28.             case MSG_FAILURE: 
  29.                 Toast.makeText(getApplication(), "下載失敗", Toast.LENGTH_LONG) 
  30.                         .show(); 
  31.                 break; 
  32.             } 
  33.         } 
  34.     };protected void onCreate(Bundle savedInstanceState) { 
  35.         super.onCreate(savedInstanceState); 
  36.         setContentView(R.layout.activity_main); 
  37.         mImageView = (ImageView) findViewById(R.id.imageView1);// 顯示圖片的ImageView 
  38.  
  39.     } 
  40.  
  41.     public void onClick(View v) { 
  42.         switch (v.getId()) { 
  43.         case R.id.button1: 
  44.             mImageView.setImageBitmap(null); 
  45.             new Thread(new Runnable() { 
  46.                 public void run() { 
  47.                     HttpClient hc = new DefaultHttpClient(); 
  48.                     HttpGet hg = new HttpGet( 
  49.                             "http://www.baidu.com/img/baidu_sylogo1.gif");// 獲取百度的LOGO 
  50.                     final Bitmap bm; 
  51.                     try { 
  52.                         HttpResponse hr = hc.execute(hg); 
  53.                         bm = BitmapFactory.decodeStream(hr.getEntity() 
  54.                                 .getContent()); 
  55.                     } catch (Exception e) { 
  56.                         mHandler.obtainMessage(MSG_FAILURE).sendToTarget();// 獲取圖片失敗 
  57.                         return; 
  58.                     } 
  59.                     mHandler.obtainMessage(MSG_SUCCESS, bm).sendToTarget();// 獲取圖片成功,向ui線程發送MSG_SUCCESS標識和bitmap對象 
  60.  
  61.                 } 
  62.             }).start(); 
  63.             break; 
  64.         } 
  65.     } 

不過這個方法有個小問題 老是提示會內存洩露

方法二:

  1. package com.example.handlerdemo; 
  2.  
  3. import org.apache.http.HttpResponse; 
  4. import org.apache.http.client.HttpClient; 
  5. import org.apache.http.client.methods.HttpGet; 
  6. import org.apache.http.impl.client.DefaultHttpClient; 
  7.  
  8. import android.app.Activity; 
  9. import android.graphics.Bitmap; 
  10. import android.graphics.BitmapFactory; 
  11. import android.os.Bundle; 
  12. import android.os.Handler; 
  13. import android.os.Message; 
  14. import android.view.View; 
  15. import android.widget.ImageView; 
  16. import android.widget.Toast; 
  17.  
  18. public class MainActivity extends Activity { 
  19.  
  20.     ImageView mImageView; 
  21.  
  22.     Handler mHandler02 = new Handler(); 
  23.  
  24.     protected void onCreate(Bundle savedInstanceState) { 
  25.         super.onCreate(savedInstanceState); 
  26.         setContentView(R.layout.activity_main); 
  27.         mImageView = (ImageView) findViewById(R.id.imageView1);// 顯示圖片的ImageView 
  28.  
  29.     } 
  30.  
  31.     public void onClick(View v) { 
  32.         switch (v.getId()) { 
  33.         case R.id.button2: 
  34.             mImageView.setImageBitmap(null); 
  35.             new Thread(new Runnable() { 
  36.                 public void run() { 
  37.                     HttpClient hc = new DefaultHttpClient(); 
  38.                     HttpGet hg = new HttpGet( 
  39.                             "http://www.baidu.com/img/baidu_sylogo1.gif");// 獲取百度的LOGO 
  40.                     try { 
  41.                         HttpResponse hr = hc.execute(hg); 
  42.                         final Bitmap bm = BitmapFactory.decodeStream(hr.getEntity() 
  43.                                 .getContent()); 
  44.                         mHandler02.post(new Runnable() { 
  45.                             public void run() { 
  46.                                 mImageView.setImageBitmap(bm);// imageview顯示從網絡獲取到的logo 
  47.                             } 
  48.                         }); 
  49.                     } catch (Exception e) { 
  50.                         mHandler02.post(new Runnable() { 
  51.                             public void run() { 
  52.                                 Toast.makeText(getApplication(), "下載失敗", 
  53.                                         Toast.LENGTH_LONG).show(); 
  54.                             } 
  55.                         }); 
  56.                     } 
  57.                      
  58.                 } 
  59.             }).start(); 
  60.             break; 
  61.         default: 
  62.             break; 
  63.         } 
  64.     } 

用方法二是不會出內存洩露的警告的

效果圖:

csdn下載地址:http://download.csdn.net/detail/wenwei19861106/4868799

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved