当用户单击主页按钮时,我想取消运行AsyncTask(在AsyncTaskLoader中)。这是我到目前为止创建的内容:
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()");
}
}
所以在onStopLoading()中我调用了cancelLoad(),我认为它应该取消当前任务,但是AsyncTaskLoader中的AsyncTask仍在运行(而loadInBackground()中的循环仍在进行中。)
问题可能在“java.android.support.v4.content.AsyncTaskLoader.java”中的cancelLoad()方法中,其中mTask.cancel(boolean)在参数中使用“false”调用:
public boolean cancelLoad() {
...
boolean cancelled = mTask.cancel(false);
是否有机会取消在AsyncTaskLoader中运行AsyncTask?
答案 0 :(得分:3)
如果你的意思是&#34;有没有机会取消在android.content.AsyncTaskLoader中运行AsyncTask?&#34;答案是肯定的:你只需要添加一些&#34;取消点&#34;在你的loadInBackground方法中检查是否已发出取消请求(isLoadInBackgroundCanceled()== true),然后返回或抛出OperationCanceledException。
您正在使用的AsyncTaskLoader的支持库版本似乎并未在此时完全实现取消(至少不在飞行途中,并且框架的粗略比较 Loader的支持版本似乎建议取消可能根本不受支持......)。
http://developer.android.com/reference/android/content/Loader.html http://developer.android.com/reference/android/support/v4/content/Loader.html
我想到了两种缓解问题的方法:
答案 1 :(得分:1)
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();
}