Android AsyncTask的理解

1. onPreExecute:主線程中,準備耗時操作

2. doInBackground : 在子線程中執行

3. onPostExecute:主線程中,耗時操作完成後,獲得數據後的回調


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

發佈了40 篇原創文章 · 獲贊 37 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章