2012-11-07 90 views
0

我註冊的廣播來獲得ConnectivityManager.CONNECTIVITY_ACTION廣播,代碼爲:安卓:ConnectivityManager.CONNECTIVITY_ACTION,發送當網連接的狀態沒有改變

registerReceiver(mNetwrokBroadcastReceiver, new IntentFilter(
      ConnectivityManager.CONNECTIVITY_ACTION)); 

和接收器是:

private BroadcastReceiver mNetwrokBroadcastReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     boolean isConnected = false; 
     NetworkInfo mNetworkInfo = intent 
       .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 
     NetworkInfo.State state = (mNetworkInfo == null ? NetworkInfo.State.UNKNOWN 
       : mNetworkInfo.getState()); 

     switch (state) { 
     case CONNECTED: 
      isConnected = true; 
      break; 

     case DISCONNECTING: 
      if (intent.getBooleanExtra(
        ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) 
       isConnected = false; 
      else { 
       isConnected = true; 
      } 
      break; 

     default: 
      break; 
     } 


     if (isConnected) { 
      mPopwin.onDismiss(); 
      init(); 

     } 
    } 
}; 

但我發現當網始終連接,它也發送廣播,爲什麼? 還有一個相同的question,我找不到它。我的意思是,我在一個活動的init中註冊一個過濾器爲「ConnectivityManager.CONNECTIVITY_ACTION」的廣播,當網絡一直連接時,我開始活動,執行函數「onReceive」;在google doc中表示「網絡連接發生了變化」,但網絡沒有變化,爲什麼要執行onReceive?

+0

你的問題不明確。請詳細說明或讓您的問題更清楚。 –

+0

好吧,我編輯了 – Mejonzhan

+0

看到我對這個問題的回答http://stackoverflow.com/a/16428823/769265 –

回答

1

由於CONNECTIVITY_ACTION是棒廣播因此它繼續呼籲onRecevie將其刪除ü可以使用removeStickyBroadcast(意向意圖)DOC