0
点赞
收藏
分享

微信扫一扫

AsyncTask例子之一

婉殇成长笔记 2023-07-31 阅读 148


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

        

1.代码

public class MyDialogActivity extends Activity implements 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 extends AsyncTask<String, Integer, String> {
   @Override
   protected void onPreExecute() {
    Log.i("tag", "onPreExecute ...");
    // 任务启动,可以在这里显示一个对话框,这里简单处理
    message.setText("onPreExecute");
    dialog = new MyDialog(MyDialogActivity.this);
    dialog.show();
    dialog.setTitle("正在下载中...");
    dialog.mSeekbar.setMax(100);
   } // 可变长的输入参数,与AsyncTask.exucute()对应
   @Override
   protected String doInBackground(String... params) {
    Log.i("tag", "doInBackground ...");
    try {
     HttpClient client = new DefaultHttpClient();
     // params[0] 代表连接的url
     HttpGet get = new HttpGet(params[0]);
     HttpResponse response = client.execute(get);
     HttpEntity entity = response.getEntity();
     long length = entity.getContentLength();
     InputStream is = entity.getContent();
     String s = null;
     if (is != null) {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      byte[] buf = new byte[128];
      int ch = -1;
      int count = 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());
     }
     // 返回结果
     return s;
    } catch (Exception e) {
     e.printStackTrace();
    }
    return null;
   } @Override
   protected void onProgressUpdate(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 void onPostExecute(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 条评论