`

安卓Handler和Message

 
阅读更多

文章来源:http://www.itnose.net/detail/6025287.html
更多文章:http://www.itnose.net/type/67.html

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <Button  
  7.         android:id="@+id/button1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentBottom="true"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:text="下载网络图片" />  
  13.   
  14.     <ImageView  
  15.         android:id="@+id/imageView1"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_centerHorizontal="true"  
  19.         android:layout_centerVertical="true"  
  20.         android:src="@drawable/ic_launcher" />  
  21.   
  22. </RelativeLayout>  


MainActivity

  1. package com.example.android_asytask_download;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.InputStream;  
  5.   
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.client.HttpClient;  
  9. import org.apache.http.client.methods.HttpGet;  
  10. import org.apache.http.impl.client.DefaultHttpClient;  
  11. import org.apache.http.util.EntityUtils;  
  12.   
  13. import android.os.AsyncTask;  
  14. import android.os.Bundle;  
  15. import android.os.Handler;  
  16. import android.os.Message;  
  17. import android.R.integer;  
  18. import android.app.Activity;  
  19. import android.app.ProgressDialog;  
  20. import android.content.Entity;  
  21. import android.graphics.Bitmap;  
  22. import android.graphics.BitmapFactory;  
  23. import android.view.Menu;  
  24. import android.view.View;  
  25. import android.widget.Button;  
  26. import android.widget.ImageView;  
  27.   
  28. public class MainActivity extends Activity {  
  29.     private Button button;  
  30.     private ImageView imageView;  
  31.     private ProgressDialog progressDialog;  
  32.     private final int IS_END = 1;  
  33.     private String image_path = "http://a.hiphotos.baidu.com/image/w%3D2048/sign=7610c70c5143fbf2c52ca1238446cb80/d4628535e5dde71120a3a1f2a5efce1b9d166145.jpg";  
  34.     private Handler handler = new Handler() {  
  35.         @Override  
  36.         public void handleMessage(android.os.Message msg) {  
  37.             byte[] data = (byte[]) msg.obj;  
  38.             Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
  39.             imageView.setImageBitmap(bitmap);  
  40.             if(msg.what==IS_END)  
  41.             {  
  42.                 progressDialog.dismiss();  
  43.             }  
  44.         };  
  45.     };  
  46.   
  47.     @Override  
  48.     protected void onCreate(Bundle savedInstanceState) {  
  49.         super.onCreate(savedInstanceState);  
  50.         setContentView(R.layout.activity_main);  
  51.   
  52.         button = (Button) this.findViewById(R.id.button1);  
  53.         imageView = (ImageView) this.findViewById(R.id.imageView1);  
  54.         progressDialog = new ProgressDialog(this);  
  55.         progressDialog.setTitle("提示");  
  56.         progressDialog.setMessage("正在下载,请稍后...");  
  57.         progressDialog.setCancelable(false);// 点击不会失去焦点,直到下载结束  
  58.   
  59.         button.setOnClickListener(new View.OnClickListener() {  
  60.   
  61.             @Override  
  62.             public void onClick(View arg0) {  
  63.                 // TODO Auto-generated method stub  
  64.                 new Thread(new MyThread()).start();  
  65.                 progressDialog.show();  
  66.             }  
  67.         });  
  68.     }  
  69.   
  70.     public class MyThread implements Runnable {  
  71.         @Override  
  72.         public void run() {  
  73.             // 线程耗时部分的主体操作  
  74.             HttpClient httpClient = new DefaultHttpClient();  
  75.             HttpGet httpGet = new HttpGet(image_path);  
  76.             try {  
  77.                 HttpResponse httpResponse = httpClient.execute(httpGet);// 执行http操作  
  78.                 if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  79.                     byte[] data = EntityUtils.toByteArray(httpResponse  
  80.                             .getEntity());  
  81.                     // 一般实例化消息对象不用new Message(),而是通过Message.obtain()  
  82.                     Message message = Message.obtain();  
  83.                     message.obj = data;  
  84.                     message.what = IS_END;  
  85.                     handler.sendMessage(message);// handler对象发送消息  
  86.                 }  
  87.             } catch (Exception e) {  
  88.                 // TODO: handle exception  
  89.             }  
  90.         }  
  91.     }  
  92.   
  93.     @Override  
  94.     public boolean onCreateOptionsMenu(Menu menu) {  
  95.         // Inflate the menu; this adds items to the action bar if it is present.  
  96.         getMenuInflater().inflate(R.menu.main, menu);  
  97.         return true;  
  98.     }  
  99.   
  100. }  


演示效果图片0演示效果图片1演示效果图片2

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

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics