AsyncTask3參數以及6方法淺析

大家都知道,AsyncTask是用來進行異步加載的,當然還有一種方式是Handler,此處就先不對Handler進行說明

首先先舉個小例子來對異步,同步進行一個說明:



首先來看看官網上的源碼:

<p>AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.</p>
以上翻譯:

AsyncTask使適當的和容易使用的UI線程。這個類允許您執行後臺操作和發佈結果在UI線程上無需操縱線程或Handler進行處理


AsyncTask is designed to be a helper class around {@link Thread} and {@link Handler} and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs
以上翻譯:
AsyncTask被設計成一個幫助類,圍繞線程和handle
不建議做耗時操作(不要做時間太久的操作),如果你要保持一個線程運行在長時間內,建議使用不同的api,自己通過線程池搭建

An asynchronous task is defined by a computation that runs on a background thread and
 whose result is published on the UI thread.
以上翻譯:
一個異步任務可以跑在後臺線程中,結果會被髮布到UI線程

AsyncTask有三個參數,六個方法,然後對這三個參數,六個方法進行說明
三個參數:Params,Progress,Result
//參數一:Params:execute傳遞
//參數二:Progress:進度
//參數三:Result是doInBackground的返回值,在onPostExecute處進行接收

六個方法:onPreExecute,doInBackground,publishProgress,onPostExecute,onProgressUpdate,execute
* execute:執行任務
        * doInBackground: 執行線程:加載數據:  run UI線程裏面
        * onPreExecute: 執行線程之前:更新UI:run 非UI線程
        * onPostExecute: 執行完線程之後:更新UI,run UI線程
        * publishProgress
        * onProgressUpdate
new AsyncTask<String,Integer,Boolean>(){
            //Boolean是參數三Result,傳遞到onPostExecute的參數中
	   // 執行線程:加載數據:run 非UI線程裏面
            @Override
            protected Boolean doInBackground(String... params) {
                Log.d("heima12",params[0]);//hello
                Log.d("heima12",params[1]);//xiaohei

                }

                return null;//此處返回true,在onPostExecute的result處進行接收
            }

        }.execute("hello","xiaohei");//此處可以傳遞多個值,傳遞"hello","小黑",在doInBackground的(String...Params)進行接收,接收處是可變參數


執行以上代碼打印Log:



	    // 執行線程之前:更新UI
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

 		@Override
            protected Boolean doInBackground(String... params) {
                }
	return null;//此處返回true,在onPostExecute的result處進行接收

	// 執行完線程之後:更新UI
 	//該方法的參數類型與doInBackground方法中Boolean的返回值類型一致
            @Override
            protected void onPostExecute(Boolean result) {
                Log.d("heima12",""+result);//true
            }


執行以上代碼打印Log:


	    //Boolean是參數三Result,傳遞到onPostExecute的參數中
            // 執行完線程之後:更新UI
            @Override
            protected Boolean doInBackground(String... params) {
                Log.d("heima12",params[0]);//hello
                Log.d("heima12",params[1]);//小黑

                for (int i = 0; i <100; i++) {
                    SystemClock.sleep(100);
                    //當有這樣一個場景,比如說下載時,後臺線程還沒有執行完,要通知更新UI,通知進度變化,提供這樣一個方法publishProgress
                    publishProgress(i/*,10*/);//參數是可變參數,i是Progress,如果是下載,下載一點,發佈一點,在onProgressUpdate處進行接收
                }

                return true;//此處返回true,在onPostExecute的result處進行接收
            }

//接收從publishProgress傳遞過來的數據
@Override
protected void onProgressUpdate(Integer... values) {
    Toast.makeText(MainActivity.this, "i="+values[0], Toast.LENGTH_SHORT).show();
}



執行以上代碼彈出Toast




以上就是對AsyncTask的中3參數以及6方法淺析.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章