Android入门教程 AsyncTask的使用及execute和executeOnExecutor方法

AsyncTask的使用分为三大步。

  1. 创建一个子类继承AsyncTask;
  2. 在子类中实现如下方法
    doInBackground()
    onProgressUpdate()
    onPostExecute()
  3. 调用execute方法(或executeOnExecutor方法)

实例:通过AsyncTask模拟下载文件

  • 通过execute方法模拟串行下载
  • 通过executeOnExecutor方法模拟并行下载

实验结果总结

通过这个实例的演示,我们可以看到使用execute方法启动时,下载任务是串行的,即必须等第一个下载完成后才能下载第二个;使用executeOnExecutor启动时,可以进行并行执行,所有任务可以同步进行。
请看下面的这张截图,我是在第三个完成、第4个刚启动时,同时启动了第6个,之后又同时启动了第7、8、9、10个任务。

注意:同一时间只能同时进行5个任务

Android入门教程 AsyncTask的使用及execute和executeOnExecutor方法_第1张图片

布局文件展示

"http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    "match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        id="@+id/p1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:max="100" />

        id="@+id/p2"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:max="100" />

        id="@+id/p3"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:max="100" />

        id="@+id/p4"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:max="100" />

        id="@+id/p5"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:max="100" />

        

Java代码展示

public class DownLoadActivity extends Activity implements OnClickListener{

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btn7;
    private Button btn8;
    private Button btn9;
    private Button btn10;

    private ProgressBar p1;
    private ProgressBar p2;
    private ProgressBar p3;
    private ProgressBar p4;
    private ProgressBar p5;
    private ProgressBar p6;
    private ProgressBar p7;
    private ProgressBar p8;
    private ProgressBar p9;
    private ProgressBar p10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_down_load);
        bindID();
    }


    private void bindID() {
        btn1 = (Button) findViewById(R.id.b1);
        btn2 = (Button) findViewById(R.id.b2);
        btn3 = (Button) findViewById(R.id.b3);
        btn4 = (Button) findViewById(R.id.b4);
        btn5 = (Button) findViewById(R.id.b5);
        btn6 = (Button) findViewById(R.id.b6);
        btn7 = (Button) findViewById(R.id.b7);
        btn8 = (Button) findViewById(R.id.b8);
        btn9 = (Button) findViewById(R.id.b9);
        btn10 = (Button) findViewById(R.id.b10);

        p1 = (ProgressBar) findViewById(R.id.p1);
        p2 = (ProgressBar) findViewById(R.id.p2);
        p3 = (ProgressBar) findViewById(R.id.p3);
        p4 = (ProgressBar) findViewById(R.id.p4);
        p5 = (ProgressBar) findViewById(R.id.p5);
        p6 = (ProgressBar) findViewById(R.id.p6);
        p7 = (ProgressBar) findViewById(R.id.p7);
        p8 = (ProgressBar) findViewById(R.id.p8);
        p9 = (ProgressBar) findViewById(R.id.p9);
        p10 = (ProgressBar) findViewById(R.id.p10);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btn10.setOnClickListener(this);
    }

    class DownloadTask extends AsyncTask{

        @Override
        protected String doInBackground(Integer... params) {
            System.out.println(params[0]+"......启动");
            String result=params[0]+"";
            int count = 0;
            while(count<10){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count++;
                publishProgress(count,params[0]);
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            System.out.println(result+"......完成");
            int flag = Integer.parseInt(result);
            switch (flag) {
            case 1:
                btn1.setText("下载完成");
                btn1.setEnabled(false);
                break;
            case 2:
                btn2.setText("下载完成");
                btn2.setEnabled(false);
                break;
            case 3:
                btn3.setText("下载完成");
                btn3.setEnabled(false);
                break;
            case 4:
                btn4.setText("下载完成");
                btn4.setEnabled(false);
                break;
            case 5:
                btn5.setText("下载完成");
                btn5.setEnabled(false);
                break;
            case 6:
                btn6.setText("下载完成");
                btn6.setEnabled(false);
                break;
            case 7:
                btn7.setText("下载完成");
                btn7.setEnabled(false);
                break;
            case 8:
                btn8.setText("下载完成");
                btn8.setEnabled(false);
                break;
            case 9:
                btn9.setText("下载完成");
                btn9.setEnabled(false);
                break;
            case 10:
                btn10.setText("下载完成");
                btn10.setEnabled(false);
                break;

            default:
                break;
            }
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            int flag = values[1];
            switch (flag) {
            case 1:
                p1.setProgress(values[0]*10);
                break;
            case 2:
                p2.setProgress(values[0]*10);
                break;
            case 3:
                p3.setProgress(values[0]*10);
                break;
            case 4:
                p4.setProgress(values[0]*10);
                break;
            case 5:
                p5.setProgress(values[0]*10);
                break;
            case 6:
                p6.setProgress(values[0]*10);
                break;
            case 7:
                p7.setProgress(values[0]*10);
                break;
            case 8:
                p8.setProgress(values[0]*10);
                break;
            case 9:
                p9.setProgress(values[0]*10);
                break;
            case 10:
                p10.setProgress(values[0]*10);
                break;

            default:
                break;
            }
        }



    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.b1:
            DownloadTask t1 = new DownloadTask();
            t1.execute(1);
            break;
        case R.id.b2:
            DownloadTask t2 = new DownloadTask();
            t2.execute(2);
            break;
        case R.id.b3:
            DownloadTask t3 = new DownloadTask();
            t3.execute(3);
            break;
        case R.id.b4:
            DownloadTask t4 = new DownloadTask();
            t4.execute(4);
            break;
        case R.id.b5:
            DownloadTask t5 = new DownloadTask();
            t5.execute(5);
            break;
        case R.id.b6:
            DownloadTask t6 = new DownloadTask();
            t6.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 6);
            break;
        case R.id.b7:
            DownloadTask t7 = new DownloadTask();
            t7.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 7);
            break;
        case R.id.b8:
            DownloadTask t8 = new DownloadTask();
            t8.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 8);
            break;
        case R.id.b9:
            DownloadTask t9 = new DownloadTask();
            t9.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 9);
            break;
        case R.id.b10:
            DownloadTask t10 = new DownloadTask();
            t10.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 10);
            break;

        default:
            break;
        }
    }

}

你可能感兴趣的:(Android入门教程,学习笔记,Android,Android入门教程)