2015-09-06 66 views
1

我正在创建一个基于http://developer.android.com/reference/android/content/AsyncTaskLoader.html的AsyncTaskLoader。根据我的日志记录,当我运行我的应用时,应用无休止地在loadInBackgroundonCanceled之间振荡。有谁知道为什么会出现这个错误?我的BroadcastReceiver基于Proper notification of AsyncTaskLoader about data changes from background threadAsyncTaskLoader 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(); 
    } 
    } 

回答

0

当它加载的背景下,其发送广播,和广播电话onContentChanged()这就是为什么当前的装载机被取消了再次运行。

您应该简单地删除BroadcastReceiver,或者BroadcastReceiver不应该调用onContentChanged()。

从您引用的链接中,BroadcastReceiver用于这样的事情,例如,您加载器将文件列表加载到一个文件夹中,出于某种原因,您知道添加了新文件,因此您发送广播(而不是加载器)并强制加载器重新运行并获取新内容。

公共无效onContentChanged()在API级别11

当Loader.ForceLoadContentObserver检测到改变时调用。 默认实现检查是否加载器当前是 已启动;如果是这样,它只是调用forceLoad();