Android AsyncTask的理解

1. onPreExecute:主线程中,准备耗时操作

2. doInBackground : 在子线程中执行

3. onPostExecute:主线程中,耗时操作完成后,获得数据后的回调

Android AsyncTask的理解_第1张图片

public class MainActivity extends Activity {

    private TextView tv;

    privateExecutorService pool; 

    @Override

    protected voidonCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        tv = (TextView)findViewById(R.id.tv); 

        pool = Executors.newFixedThreadPool(3);

    }

 

    public void start(Viewview) {

 

        // 3.0之前,同时执行多个线程

        // 3.0之后,单线程

 

        // 做耗时操作获得变化后的数据   

        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {

            // 3.0以下

            // @1: 对应的参数是doInBackground回调中传入的参数,执行任务的参数

            // @2: 进度,onProgressUpdate的参数类型

            // @3:doInBackground的返回值类型,onPostExecute传入的参数类型

            newAsyncTask<Integer, Integer, Integer>() {

                @Override

                protectedvoid onPreExecute() {

                    // 主线程中, 准备执行前的回调 

                }

 

                @Override

                protectedInteger doInBackground(Integer... params) {

                    // 子线程,耗时操作

                    intstart = params[0];

                    intend = params[1];

 

                    intresult = 0;

                    for(int i = start; i <= end; i++) {

 

                        try{

                            Thread.sleep(50);

                        }catch (InterruptedException e) {

                            e.printStackTrace();

                        }

                        result= i;

 

                        publishProgress(result);

                    } 

                    return result;

                }

 

                @Override

                protectedvoid onProgressUpdate(Integer[] values) {

                    // 主线程中执行, 进度改变时的回调 

                    intprogress = values[0];

                    // UI更新

                    tv.setText(progress+ "");

                };

 

                @Override

                protectedvoid onPostExecute(Integer result) {

                    // 主线程中,执行完成的回调 

                    // 改变UI

                    tv.setText(result+ ""); 

                }

            }.execute(0,100);

        } else {

            // @1: 对应的参数是doInBackground回调中传入的参数,执行任务的参数

            // @2: 进度,onProgressUpdate的参数类型

            // @3:doInBackground的返回值类型,onPostExecute传入的参数类型

            newAsyncTask<Integer, Integer, Integer>() {

                @Override

                protectedvoid onPreExecute() {

                    // 主线程中, 准备执行前的回调 

                }

 

                @Override

                protectedInteger doInBackground(Integer... params) {

                    // 子线程,耗时操作

                    intstart = params[0];

                    intend = params[1]; 

                    intresult = 0;

                    for(int i = start; i <= end; i++) { 

                        try{

                            Thread.sleep(50);

                        }catch (InterruptedException e) {

                            e.printStackTrace();

                        }

                        result= i; 

                        publishProgress(result);

                    } 

                    return result;

                }

 

                @Override

                protectedvoid onProgressUpdate(Integer[] values) {

                    // 主线程中执行, 进度改变时的回调 

                    intprogress = values[0];

                    // UI更新

                    tv.setText(progress+ "");

                };

 

                @Override

                protectedvoid onPostExecute(Integer result) {

                    // 主线程中,执行完成的回调 

                    // 改变UI

                    tv.setText(result+ ""); 

                }

            }.executeOnExecutor(pool,0, 100);

        }

 

    }

 

}


Demo下载地址:http://download.csdn.net/detail/sanyang730/9515520

你可能感兴趣的:(android,AsyncTask)