2015-06-11 33 views
5

也就是說,我有一個廣播接收機,用於在一個廣播中收聽廣播,之後我希望它自己取消註冊。可以從接收者自己的onReceive()方法註銷'dynamic'BroadcastReceiver嗎?

我還沒有找到任何這樣做的示例代碼,但是我沒有在android在線文檔中發現任何禁止此操作的規則。但是我不能讓它像活動一樣長時間滯留,而且它仍然處於匿名類中,所以包含類甚至不知道變量名。

也就是說,代碼看起來是這樣的:

myInfoReceiver = new BroadcastReceiver() { 
onReceive(Context ctx, Intent intt) { 
    // do some Notification when I get here 
    nm.notify("I got here") // obvious pseudo code 
    ctx.unregisterReceiver(myInfoReceiver); 
} // end onReceive 
ctx.registerReceiver),uInfoReceiver, new IntentFilter(...)); 
}; // end BroadcastReceiver 

但是當我運行這一點,Android的時,它調用註銷,堅持接收機是不是有註銷(我忘了確切的措辭抱怨,但它拋出IllegalArgumentException)。

我也嘗試修改代碼來檢查'intt'中的動作是否與預期相同 - 但是它仍然執行onReceive,但是默默無法取消註冊。

+0

什麼你的目標是什麼想要實現?我想,'BroadcastReceiver'可能是您的問題的錯誤選擇。如果您想要執行一次操作並且想要使其與活動生命週期無關,請考慮在事件總線(例如Otto)中使用「IntentService」或「Service」。 –

+0

我不會使用IntentService等,因爲這是一部分的較大的系統已經有一個廣播意圖,恰好代表我想要的信息。另外,我不需要生命週期中的那麼多獨立性。我已經在UI線程中等待這個意圖,什麼都不做,只是顯示一個進度對話框。 –

回答

5

您的問題的答案是「是」。但是...

...您需要撥打unregisterReceiver()就可以撥打registerReceiver()上的Context。在您發佈的代碼中,您調用Context時調用unregisterReceiver()作爲onReceive()的參數。這與Context不一樣,這就是你獲得例外的原因。

+0

我一直懷疑這種情況已有一段時間了。謝謝你的確定性。 –

+0

我現在正面臨這個問題。有沒有很好的解決方法? – user2892437

0

我只是說YES,完全沒問題。

例如,我將它用於一次性位置修復,並且可以在其他邏輯中使用它,而不會看到任何問題。

另外我多次見過它。

+0

我會認爲「沒有問題」。但是我得到IllegalArgumentException。任何想法爲什麼?它如何不能在它自己的onReceive()方法中註冊? –

+0

您是否正在傳送正確的接收器對象,同時取消註冊接收器,因爲我可以看到您發佈的上述代碼中的不匹配 – 7383

+0

您只需要在相同的「上下文」中調用此對象。例如,使用'MyActivity.this'這樣的'Activity'。否則,請發佈堆棧跟蹤。 – shkschneider

0

我嘗試了各種解決方案,最後我做這樣的說法:

註冊:

MyApplication.getInstance().getApplicationContext().registerReceiver(sentReceiver, new IntentFilter(SENT)); 

SentReceiver:

public class SentReceiver extends BroadcastReceiver { 
    public void onReceive(Context context, Intent arg1) { 
     switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       Toast.makeText(context, 
         context.getString(R.string.sms_envoye), Toast.LENGTH_SHORT) 
         .show(); 
       break; 
      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
       Toast.makeText(context, 
         context.getString(R.string.sms_defaillance_generique), 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NO_SERVICE: 
       Toast.makeText(context, 
         context.getString(R.string.sms_pas_de_service), 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NULL_PDU: 
       Toast.makeText(context, 
         context.getString(R.string.sms_pas_de_pdu), 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_RADIO_OFF: 
       Toast.makeText(context, 
         context.getString(R.string.sms_radio_desactivee), 
         Toast.LENGTH_SHORT).show(); 
       break; 
     } 
     MyApplication.getInstance().getApplicationContext().unregisterReceiver(this); 
    } 

隨着所有MyApplication:

public class MyApplication extends Application { 
    private static MyApplication mInstance; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mInstance = this; 
    } 

    public static synchronized MyApplication getInstance() { 
     return mInstance; 
    }   
} 
相關問題