标签:
<span style="color:#660000;"> package com.hqyj.asyctask; import android.os.AsyncTask; import android.widget.TextView; //Params 启动任务时的输入参数类型。注意:该参数只能填写引用数据类型,如果没有参数输入,填写Void //Progress 后台任务执行的进度的类型。 //Result 后台计算结果的类型。 public class MyAsyncTask extends AsyncTask<Integer, Integer, String> { <span style="white-space: pre;"> </span>TextView tv; <span style="white-space: pre;"> </span>int count; <span style="white-space: pre;"> </span>public MyAsyncTask(TextView tv) { <span style="white-space: pre;"> </span> // TODO Auto-generated constructor stub <span style="white-space: pre;"> </span> this.tv = tv; <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span> //该方法被子线程调用 <span style="white-space: pre;"> </span>//将耗时的操作放在该方法中 <span style="white-space: pre;"> </span>@Override <span style="white-space: pre;"> </span>protected String doInBackground(Integer... params) { <span style="white-space: pre;"> </span> // TODO Auto-generated method stub <span style="white-space: pre;"> </span> while(count < params[0]){ <span style="white-space: pre;"> </span> //要求系统调用onProgressUpdate方法。 <span style="white-space: pre;"> </span> try { <span style="white-space: pre;"> </span> Thread.sleep(1000); <span style="white-space: pre;"> </span> } catch (InterruptedException e) { <span style="white-space: pre;"> </span> // TODO Auto-generated catch block <span style="white-space: pre;"> </span> e.printStackTrace(); <span style="white-space: pre;"> </span> } <span style="white-space: pre;"> </span> count++; <span style="white-space: pre;"> </span> this.publishProgress(count); <span style="white-space: pre;"> </span> } <span style="white-space: pre;"> </span> return "任务结束"; <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span> //该方法被UI主线程调用 <span style="white-space: pre;"> </span> // <span style="white-space: pre;"> </span>@Override <span style="white-space: pre;"> </span>protected void onProgressUpdate(Integer... values) { <span style="white-space: pre;"> </span> // TODO Auto-generated method stub <span style="white-space: pre;"> </span> super.onProgressUpdate(values); <span style="white-space: pre;"> </span> tv.setText(String.valueOf(values[0])); <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span> //异步任务启动时调用 <span style="white-space: pre;"> </span>@Override <span style="white-space: pre;"> </span>protected void onPreExecute() { <span style="white-space: pre;"> </span> // TODO Auto-generated method stub <span style="white-space: pre;"> </span> super.onPreExecute(); <span style="white-space: pre;"> </span> tv.setText("任务开始"); <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>//异步任务结束时调用 <span style="white-space: pre;"> </span>@Override <span style="white-space: pre;"> </span> protected void onPostExecute(String result) { <span style="white-space: pre;"> </span> // TODO Auto-generated method stub <span style="white-space: pre;"> </span> super.onPostExecute(result); <span style="white-space: pre;"> </span>tv.setText(result); }</span>
android线程之 Handler与AsyncTask的应用
标签:
原文地址:http://blog.csdn.net/u011401496/article/details/42031639