[view] 10_AsyncTask案例解释

Android 4.0

AsyncTask案例解释

1、布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="点击下载" />
    <ProgressBar
        android:id="@+id/pb"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:scrollbarSize="50dp" />
</LinearLayout>
2、核心代码:
package cn.zengfansheng.asyncDownload;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
    private ProgressBar pb;
    private List<Student> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb = (ProgressBar) this.findViewById(R.id.pb);
    }
    public void click(View view) {
        // 使用AsyncTask开启异步任务
        MyAsyncTask myAsyncTask = new MyAsyncTask();
        myAsyncTask.execute("240");
    }
    /**
     * 第一个参数: 用户传递的参数类型
     * 第二个参数: 用于推进进度显示的值,一直定义为Integer类型
     * 第三个参数: 想要的结果类型
     * @author hacket
     */
    private class MyAsyncTask extends AsyncTask<String, Integer, List<Student>> {
        /**
         * 后台耗时任务执行前执行 执行在主线程中
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            list = new ArrayList<Student>();
            Toast.makeText(getApplicationContext(), "开始下载~", Toast.LENGTH_SHORT).show();
        }
        // 后台耗时任务执行在这里,执行在子线程中
        @Override
        protected List<Student> doInBackground(String... params) {// 参数一,返回参数二给onPostExecute接收
            System.out.println("params:" + params[0]);
            pb.setMax(100);
            for (int i = 1; i <= 100; i++) {
                String sex = (i%2==0?"male":"female");
                Student student = new Student(String.valueOf(i),"hacket_"+i,sex);
                list.add(student);
                publishProgress(i);// 发布进度值(参数二),onProgressUpdate接收
                SystemClock.sleep(100);
            }
            return list;
        }
        // 发布进度更新,一般用于进度条,进度对话框等的进度值更新,运行在主线程
        @Override
        protected void onProgressUpdate(Integer... values) {// 参数二
            super.onProgressUpdate(values);
            int progress = values[0];
            pb.setProgress(progress);
        }
        // 耗时任务完成后执行,运行在主线程
        @Override
        protected void onPostExecute(List<Student> result) {// 参数三:结果值
            super.onPostExecute(result);
            List<Student> students = result;
            for (Student student : students) {
                System.out.println(student);
            }
            Toast.makeText(getApplicationContext(), "下载完毕~", Toast.LENGTH_SHORT).show();
        }
    }
}
3、结果: