安卓-多線程知識(shí)

AsyncTask

http://developer.android.youdaxue.com/reference/android/os/AsyncTask.html

public abstract class AsyncTask extends [Object](http://developer.android.youdaxue.com/reference/java/lang/Object.html) 
[java.lang.Object](http://developer.android.youdaxue.com/reference/java/lang/Object.html)
android.os.AsyncTask<Params, Progress, Result>

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.
AsyncTask is designed to be a helper class around Thread and 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 provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

AsyncTask 是易于使用的 Android 類,它允許在后臺(tái)線程上執(zhí)行任務(wù),從而不會(huì)中斷主線程。要使用 AsyncTask,你應(yīng)該定義它的子類,就像我們對(duì) FetchWeatherTask 所做的那樣。有四個(gè)重要的方法會(huì)被重載:

  • onPreExecute - 在任務(wù)啟動(dòng)前,此方法在 UI 上運(yùn)行,并且負(fù)責(zé)任何需要完成的設(shè)置。
  • doInBackground - 這是你要脫離主線程完成的實(shí)際任務(wù)的代碼。它將在后臺(tái)線程上運(yùn)行,并且不會(huì)中斷 UI。
  • onProgressUpdate - 此方法在 UI 線程上運(yùn)行,并且用于顯示任務(wù)的進(jìn)度(例如顯示加載條動(dòng)畫)。
  • onPostExecute - 在任務(wù)完成之后,此方法在 UI 上運(yùn)行。

請(qǐng)注意,在啟動(dòng) AsyncTask 后,它會(huì)關(guān)聯(lián)到啟動(dòng)它的 Activity 。在 Activity 被銷毀時(shí)(例如旋轉(zhuǎn)手機(jī)時(shí)),你啟動(dòng)的 AsyncTask 將引用被銷毀的 Activity 而不是新創(chuàng)建的 Activity。這就是 AsyncTask 用于長(zhǎng)時(shí)間運(yùn)行的任務(wù)很危險(xiǎn)的原因之一。

AsyncTaskLoader

啟動(dòng)
getSupportLoaderManager().initLoader(FORECAST_LOADER_ID, bundleForLoader, callback);

刷新
getSupportLoaderManager().restartLoader(FORECAST_LOADER_ID, bundleForLoader, this);

緩存結(jié)果

   return new AsyncTaskLoader<String[]>(this) {

            /* This String array will hold and help cache our weather data */
            String[] mWeatherData = null;

            // COMPLETED (3) Cache the weather data in a member variable and deliver it in onStartLoading.
            /**
             * Subclasses of AsyncTaskLoader must implement this to take care of loading their data.
             */
            @Override
            protected void onStartLoading() {
                if (mWeatherData != null) {
                    deliverResult(mWeatherData);
                } else {
                    mLoadingIndicator.setVisibility(View.VISIBLE);
                    forceLoad();
                }
            }

            ...
            ...
            ...

            /**
             * Sends the result of the load to the registered listener.
             *
             * @param data The result of the load
             */
            public void deliverResult(String[] data) {
                mWeatherData = data;
                super.deliverResult(data);
            }
        };
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android開發(fā)者:你真的會(huì)用AsyncTask嗎? 導(dǎo)讀.1 在Android應(yīng)用開發(fā)中,我們需要時(shí)刻注意保證...
    cxm11閱讀 2,773評(píng)論 0 29
  • Android Handler機(jī)制系列文章整體內(nèi)容如下: Android Handler機(jī)制1之ThreadAnd...
    隔壁老李頭閱讀 3,420評(píng)論 1 15
  • 官方概述 AsyncTask enables proper and easy use of the UI thre...
    備忘君閱讀 950評(píng)論 0 1
  • 開發(fā)中如果遇到對(duì)象有多種屬性時(shí),構(gòu)造方法即可能攜帶大量參數(shù),這些參數(shù)會(huì)降低代碼的可讀性,不查看源碼可能無法分辨各個(gè)...
    caym閱讀 491評(píng)論 0 0
  • 以前我覺得我的兄弟是個(gè)特別夸張的人,你說哪有見到好看的姑娘立馬就兩眼放光的人!他就是!每見到好看的姑娘,他都要評(píng)頭...
    但愿撲火之蛾成佛閱讀 313評(píng)論 0 1

友情鏈接更多精彩內(nèi)容