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?
你的問題不明確。請詳細說明或讓您的問題更清楚。 –
好吧,我編輯了 – Mejonzhan
看到我對這個問題的回答http://stackoverflow.com/a/16428823/769265 –