Cannot cancel running AsyncTask in AsyncTaskLoader

 ̄綄美尐妖づ 提交于 2019-12-18 18:27:11

问题


I want to cancel running AsyncTask (in AsyncTaskLoader) when the user clicks the home button. Here is what I created so far:

package cz.davidliska.android.loaders;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.util.Log;

public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Date> {
    private static final String TAG = "loader";
    private Date date;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LoaderManager.enableDebugLogging(true);
        getSupportLoaderManager().initLoader(0, savedInstanceState, this);
    }

    private static class DateLoader extends AsyncTaskLoader<Date> {
        public static final String STATE_DATE_LOADER = "dateloader.state";
        private Date mDate;

        public DateLoader(Context context, Bundle savedInstanceState) {
            super(context);
            if (savedInstanceState != null) {
                long timeStamp = savedInstanceState.getLong(STATE_DATE_LOADER);
                if (timeStamp != 0L) mDate = new Date(timeStamp);
            }
        }

        @Override
        protected void onStartLoading() {
            Log.d(TAG, "Loader.onStartLoading()");
            if (mDate != null) {
                deliverResult(mDate);
            } else {
                forceLoad();
            }
        }

        @Override
        public void deliverResult(Date data) {
            super.deliverResult(data);
            Log.d(TAG, "Loader.deliverResult()");
            mDate = data;
        }

        @Override
        protected void onStopLoading() {
            super.onStopLoading();
            cancelLoad(); // try to cancel AsyncTask
            Log.d(TAG, "Loader.onStopLoading()");
        }


        @Override
        protected void onForceLoad() { // overridden in AsyncTaskLoader
            super.onForceLoad();
            Log.d(TAG, "Loader.onForceLoad()");
        }

        @Override
        protected void onReset() {
            super.onReset();
            mDate = null;
            Log.d(TAG, "Loader.onForceLoad()");
        }

        @Override
        public void onContentChanged() {
            super.onContentChanged();
            Log.d(TAG, "Loader.onContentChanged()");
        }

        @Override
        protected void onAbandon() {
            super.onAbandon();
            Log.d(TAG, "Loader.onContentChanged()");
        }

        @Override
        public Date loadInBackground() { // AsyncTaskLoader only
            Log.d(TAG, "Loader.loadInBackground()");
            try {
                // there will be some HttpClient.execute()
                while(true) {
                    Thread.sleep(100);
                    Log.d(TAG, "Loader.loadInBackground() still running");
                }

            } catch (InterruptedException e) {
                Log.d(TAG, "Loader.loadInBackground() interupted");
            }
            Log.d(TAG, "Loader.loadInBackground() finished");
            return new Date();
        }

        @Override
        public void onCanceled(Date data) { // AsyncTaskLoader only
            super.onCanceled(data);
            Log.d(TAG, "Loader.onCanceled()");
        }

        @Override
        protected Date onLoadInBackground() { // AsyncTaskLoader only
            Log.d(TAG, "Loader.onContentChanged()");
            return super.onLoadInBackground();
        }

        @Override
        public void abandon() {
            Log.d(TAG, "Loader.abandon()");
            super.abandon();
        }

        @Override
        public boolean cancelLoad() {
            Log.d(TAG, "Loader.cancelLoad()");
            return super.cancelLoad();
        }

        @Override
        public void forceLoad() {
            Log.d(TAG, "Loader.forceLoad()");
            super.forceLoad();
        }

        @Override
        public boolean isAbandoned() {
            Log.d(TAG, "Loader.isAbandoned()");
            return super.isAbandoned();
        }

        @Override
        public boolean isReset() {
            Log.d(TAG, "Loader.isReset()");
            return super.isReset();
        }

        @Override
        public boolean isStarted() {
            Log.d(TAG, "Loader.isStarted()");
            return super.isStarted();
        }

        @Override
        public void reset() {
            Log.d(TAG, "Loader.reset()");
            super.reset();
        }

        @Override
        public void stopLoading() {
            Log.d(TAG, "Loader.stopLoading()");
            super.stopLoading();
        }

        @Override
        public boolean takeContentChanged() {
            Log.d(TAG, "Loader.takeContentChanged()");
            return super.takeContentChanged();
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (date != null)
            outState.putLong(DateLoader.STATE_DATE_LOADER, date.getTime());
    }

    public Loader<Date> onCreateLoader(int id, Bundle args) {
        Log.d(TAG, "LoaderCallback.onCreateLoader()");
        if (args != null) Log.d(TAG, "bundle: " + args.getLong(DateLoader.STATE_DATE_LOADER));
        return new DateLoader(this, args);
    }

    public void onLoadFinished(Loader<Date> loader, Date data) {
        Log.d(TAG, "LoaderCallback.onLoadFinished(): " + new SimpleDateFormat("dd/MM/yyyy").format(data));
        date = data;
    }

    public void onLoaderReset(Loader<Date> loader) {
        Log.d(TAG, "LoaderCallback.onLoaderReset()");
    }
}

So in onStopLoading() i am calling cancelLoad(), which i think should cancel current task, but AsyncTask in AsyncTaskLoader is still running (while loop in loadInBackground() is still in progress).

The problem is maybe in cancelLoad() method in "java.android.support.v4.content.AsyncTaskLoader.java", where mTask.cancel(boolean) is called with "false" in arguments:

public boolean cancelLoad() {
    ...
    boolean cancelled = mTask.cancel(false);

Is there any chance to cancel running AsyncTask in AsyncTaskLoader?


回答1:


If you mean "Is there any chance to cancel running AsyncTask in android.content.AsyncTaskLoader?" the answer is yes: you just need to add some "cancel points" in your loadInBackground method and check whether a cancellation request has been issued (isLoadInBackgroundCanceled() == true), then either return or throw an OperationCanceledException).

The support library version of AsyncTaskLoader you are using though doesn't seem to fully implement cancellation at this time (not in mid-flight at least, and a cursory comparison of the framework and of the support version of Loader seems to suggest that cancellation might not be supported at all...).

http://developer.android.com/reference/android/content/Loader.html http://developer.android.com/reference/android/support/v4/content/Loader.html

Two ways to alleviate the problem come to my mind:

  • create two implementations of your Loader (one using the framework for API level 11 and above, and one without the cancel feature for older devices)
  • create an android.support.v4.content.Loader subclass and handle each AsyncTask and cancellation request yourself



回答2:


public boolean cancelLoad() {
    ...
    boolean cancelled = mTask.cancel(false);
}



        @Override
        public Date loadInBackground() { // AsyncTaskLoader only
            Log.d(TAG, "Loader.loadInBackground()");

            try {
                // there will be some HttpClient.execute()
                while(true) {
                    Thread.sleep(100);
                    Log.d(TAG, "Loader.loadInBackground() still running");

                   if(cancelled)                 <---------
                      return new Date();         <----------
                }

            } catch (InterruptedException e) {
                Log.d(TAG, "Loader.loadInBackground() interupted");
            }
            Log.d(TAG, "Loader.loadInBackground() finished");
            return new Date();
        }


来源:https://stackoverflow.com/questions/11612530/cannot-cancel-running-asynctask-in-asynctaskloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!