项目中用到的handler异步加载

[java]  view plain copy print ?
  1. package com.testasyntextview;  
  2. /** 
  3.  * 把获取的线程写到方法中(比较好) 
  4.  */  
  5. import android.app.Activity;  
  6. import android.app.ProgressDialog;  
  7. import android.content.Context;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.text.Html;  
  12. import android.text.Spanned;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.TextView;  
  17.   
  18. public class TestAsynTextViewActivity extends Activity {  
  19.     private TextView textView1;  
  20.     private Button button1;  
  21.     private Context context;  
  22.     private ProgressDialog progressDialog;  
  23.     private Spanned html;  
  24.   
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.         context = this;  
  31.         textView1 = (TextView) findViewById(R.id.textView1);  
  32.         button1 = (Button) findViewById(R.id.button1);  
  33.         button1.setOnClickListener(l);  
  34.   
  35.     }  
  36.   
  37.     private OnClickListener l = new OnClickListener() {  
  38.   
  39.         @Override  
  40.         public void onClick(View v) {  
  41.   
  42.             progressDialog = ProgressDialog.show(context, "获取数据中""等待");  
  43.             getHtmlDate();  
  44.   
  45.         }  
  46.     };  
  47.   
  48.     private void getHtmlDate() {// 获取数据,把线程写入了其中  
  49.         new Thread() {  
  50.             public void run() {  
  51.                 Message msg = myHandler.obtainMessage();  
  52.                 try {  
  53.                     html = HttpUtil.fromHtml(HttpUtil  
  54.                             .getHtml("http://wap.sina.com"));  
  55.                     msg.what = 0;  
  56.                 } catch (Exception e) {  
  57.                     e.printStackTrace();  
  58.                     msg.what = 1;  
  59.                 }  
  60.   
  61.                 myHandler.sendMessage(msg);  
  62.             }  
  63.         }.start();  
  64.     }  
  65.   
  66.     Handler myHandler = new Handler() {  
  67.   
  68.         public void handleMessage(Message msg) {  
  69.             switch (msg.what) {  
  70.             case 0:  
  71.                 textView1.setText(html);  
  72.                 progressDialog.dismiss();  
  73.                 break;  
  74.             case 1:  
  75.                 textView1.setText("当前无数据");  
  76.                 progressDialog.dismiss();  
  77.                 break;  
  78.             }  
  79.             super.handleMessage(msg);  
  80.         }  
  81.     };  
  82.   
  83. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值