AsyncTask例子之一

来源:互联网 发布:java过滤特殊字符xss 编辑:程序博客网 时间:2024/06/06 17:55

功能描述: 点击按钮从网络取文本文件显示

         从网络取文本文件采用AsyncTask实现 同时显示网络加载文本进度

1.代码

 public class MyDialogActivity extends Activityimplements OnClickListener {
 private MyDialog dialog;
 private TextView message;
 private ImageView btnDown;
 private PageTask pageTask;

 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  findView();
 }

 public void findView() {
  message = (TextView)findViewById(R.id.main_textview);
  btnDown = (ImageView)findViewById(R.id.down_button);
  btnDown.setOnClickListener(this);
 }

 public void onClick(View v) {
  Log.e("tag", "onClick...");
  if (v.getId() ==R.id.down_button) {
   pageTask =new PageTask();
   pageTask.execute("http://192.168.1.100:88/novel.txt");
  }
 }

 @Override
 public void onDestroy() { //此处报错难到是任务开始就不准停吗?
//  if (pageTask.getStatus() !=AsyncTask.Status.FINISHED) { // 如果没有完成
//   pageTask.cancel(true);// 停止该任务
//  }

 }

 class PageTask extendsAsyncTask<String, Integer, String>{
  @Override
  protected void onPreExecute(){
   Log.i("tag","onPreExecute ...");
   //任务启动,可以在这里显示一个对话框,这里简单处理
   message.setText("onPreExecute");
   dialog = newMyDialog(MyDialogActivity.this);
   dialog.show();
   dialog.setTitle("正在下载中...");
   dialog.mSeekbar.setMax(100);
  }

  //可变长的输入参数,与AsyncTask.exucute()对应
  @Override
  protected StringdoInBackground(String... params) {
   Log.i("tag","doInBackground ...");
   try {
    HttpClientclient = new DefaultHttpClient();
    //params[0] 代表连接的url
    HttpGetget = new HttpGet(params[0]);
    HttpResponseresponse = client.execute(get);
    HttpEntityentity = response.getEntity();
    longlength = entity.getContentLength();
    InputStreamis = entity.getContent();
    Strings = null;
    if(is != null) {
     ByteArrayOutputStreambaos = new ByteArrayOutputStream();
     byte[]buf = new byte[128];
     intch = -1;
     intcount = 0;
     while((ch = is.read(buf)) != -1) {
      baos.write(buf,0, ch);
      count+= ch;
      if(length > 0) {
       //如果知道响应的长度,调用publishProgress()更新进度
       publishProgress((int)((count / (float) length) * 100));
      }
      //为了在模拟器中清楚地看到进度,让线程休眠100ms
      Thread.sleep(100);
     }
     s= new String(baos.toByteArray());
    }
    //返回结果
    returns;
   } catch(Exception e) {
    e.printStackTrace();
   }
   returnnull;
  }

  @Override
  protected voidonProgressUpdate(Integer... values) {
   Log.i("tag","onProgressUpdate ...values[0] :" + values[0]);
   // 更新进度
   //message.setText(values[0]);
   dialog.mDialogTitle.setText(values[0]+ "%");
   dialog.mSeekbar.setProgress(values[0]);
  }

  @Override
  protected voidonPostExecute(String result) {
   Log.i("tag","onPostExecute ...");
   //返回HTML页面的内容
   message.setText(result);
   dialog.dismiss();
  }

  @Override
  protected void onCancelled(){
   super.onCancelled();
  }
 }

}

 

<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
   <ImageView
   android:id="@+id/down_button"
   android:paddingLeft="6.0dip"
   android:paddingTop="6.0dip"
   android:paddingRight="1.0dip"
   android:paddingBottom="5.0dip"
   android:layout_width="60.0dip"
   android:layout_height="50.0dip"
   android:src="@drawable/down_normal"
   >
  </ImageView>
    <TextView 
     android:id="@+id/main_textview"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
    />
    
</LinearLayout>

0 0