2015-09-06 55 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();