我正在創建一個基於http://developer.android.com/reference/android/content/AsyncTaskLoader.html的AsyncTaskLoader。根據我的日誌記錄,當我運行我的應用時,應用無休止地在loadInBackground
和onCanceled
之間振盪。有誰知道爲什麼會出現這個錯誤?我的BroadcastReceiver基於Proper notification of AsyncTaskLoader about data changes from background thread。AsyncTaskLoader endless onCanceled調用
這是我的loadInBackground方法:
@Override
public List<MyItem> loadInBackground() {
List<MyItem> items = createDummyData();
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent(RECEIVER_FILTER_STRING));
Log.d(TAG,」custom loader send broadcast from background and send items: "+items.size());
return items;
}
這是我的onStartLoading
@Override
protected void onStartLoading() {
Log.d(TAG, "items loader onStartLoading");
if (null != mData) {
Log.d(TAG, "items loader onStartLoading mData not null");
//someone is calling to start the loader, so if we have data, deliver it now
deliverResult(mData);
}
if (null == mReceiver) {
Log.d(TAG, "items loader onStartLoading register receiver");
mReceiver = new LoaderBroadcastReceiver(this);
LocalBroadcastManager.getInstance(getContext()).
registerReceiver(mReceiver, new IntentFilter(RECEIVER_FILTER_STRING));
}
if (takeContentChanged() || null == mData) {
//if data has changed since the last time it was loaded or is not available, then:
Log.d(TAG, "items loader onStartLoading onChange forceLoad");
forceLoad();
}
}
這是我的接收機
class LoaderBroadcastReceiver extends BroadcastReceiver {
private Loader loader;
public LoaderBroadcastReceiver(Loader loader) {
this.loader = loader;
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "loader receiver informing of oonContentChagned");
loader.onContentChanged();
}
}