2017-01-24 62 views
0

我目前的Android應用程序使用IntentService來調用SOAP WebService。您是否必須在Android應用程序中註銷IntentService Broadcast接收器

IntentService從其onHandleIntent()方法內返回經由LocalBroadcastManager SOAP響應數據給調用活動

final Intent localIntent = new Intent(FASTEST_BROADCAST_ACTION); 
localIntent.putExtra(FASTEST_EXTENDED_DATA, xmlUtility.getFastestServiceId()); 
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(localIntent); 

我的呼叫活動在其onResume()方法如下登記的廣播聽衆

@Override 
protected void onResume() { 
     super.onResume(); 

     mDepartureBroadcastReceiver = new DepartureBroadcastReceiver(); 
     final IntentFilter statusIntentFilter = new IntentFilter(FASTEST_BROADCAST_ACTION); 
     LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mDepartureBroadcastReceiver, statusIntentFilter); 
     Log.v(LOG_TAG, "Register receiver = " + mDepartureBroadcastReceiver); 

    } 

一切正常,Web服務被調用並且所需的響應數據被返回給我的活動。

問題是當我離開當前活動並嘗試調用unregisterReceiver

我有這樣的代碼在我方法: -

/** 
    * Dispatch onPause() to fragments. 
    */ 
    @Override 
    protected void onPause() { 
     super.onPause(); 

     if (mDepartureBroadcastReceiver == null) { 
      //INTENTIONALLY LEFT BLANK 
     } else { 
      try { 
       Log.v(LOG_TAG, "About to unregister receiver"); 
       unregisterReceiver(mDepartureBroadcastReceiver); 
       Log.v(LOG_TAG, "Successfully unregistered receiver"); 
      } catch (final Exception exception) { 
       Log.e(LOG_TAG, "Failed unregistered receiver", exception); 
      } finally { 
       mDepartureBroadcastReceiver = null; 
      } 
     } 
    } 

我總是得到這樣的例外: -

Failed unregistered receiver java.lang.IllegalArgumentException: Receiver not registered 

收件人註冊: -

註冊接收器= [email protected]

匹配例外: -

java.lang.IllegalArgumentException: Receiver not registered: [email protected] 

爲什麼這個異常發生的呢?

爲什麼不能註銷這個接收器?

是否由於我的IntentService完成(例如調用其onDestroy()方法)而導致我調用取消註冊?

回答

1

您不是使用LocalBroadcastManager註銷。

您需要使用此LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(mDepartureBroadcastReceiver);

+0

好去處!我不知道我怎麼錯過了。謝謝 – Hector

相關問題