我試圖從廣播接收器中顯示一個警報對話框片段,但接收器不在將顯示片段的活動中(接收器處理在此事件上廣播的所有錯誤,無論是否是主動活動)。檢索廣播接收器中的調用上下文()
這是我目前的接收器:
private BroadcastReceiver mHttpPostReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra("Error") != null) { // error occurred
// called from a fragment in another activity
NetworkError.show(((Activity) context).getFragmentManager(), error);
}
}
};
但在功能上下文參數是它是在並導致運行時非法狀態異常,因爲這個活動是不是在屏幕上活動的當前上下文。有沒有辦法通過廣播發送調用函數的上下文?或者還有另一種方法我應該實施這個?
我目前得到特定的錯誤是:
java.lang.IllegalStateException:不能
的onSaveInstanceState
這會工作,但調用這一個的活動不會暫停。調用活動發送廣播然後空閒,上面的方法現在創建一個SharedPreferences條目,但調用函數以後不會到達onResume方法。 – user1988523
@ user1988523你是什麼意思* idles *?你的意思是Activity的'onPause()'沒有被調用? –
這似乎是這種情況。我在調用函數的onPause()和onResume()中設置了斷點,但它們從未到達。在此期間,調用片段創建一個線程,該線程發送廣播,然後由另一個活動中的上述函數接收廣播(原始片段在發送廣播後不做任何操作)。 – user1988523