不要以为Wifi的状态只有2个!<wbr></wbr>
Android下Wifi的状态实际上有5个,依次为:
0 --> WIFI_STATE_DISABLING
1 --> WIFI_STATE_DISABLED
2 --> WIFI_STATE_ENABLING
3 --> WIFI_STATE_ENABLED
4 --> WIFI_STATE_UNKNOWN
<wbr></wbr>
每当该状态有变化时,系统都会发出“WIFI_STATE_CHANGED_ACTION”广播事件。
<wbr></wbr>
以下的receiver仅供参考:
(注册过程如下:
<wbr><wbr><wbr><wbr> mWifiStateReceiver = new WifiStateReceiver();</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> registerReceiver(mWifiStateReceiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));</wbr></wbr></wbr></wbr>
注销过程:
<wbr><wbr><wbr><wbr> unregisterReceiver(mWifiStateReceiver);<br></wbr></wbr></wbr></wbr>
)
<wbr></wbr>
class WifiStateReceiver extends BroadcastReceiver {<wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>public void onReceive(Context c, Intent intent) {<br><wbr><wbr><wbr><wbr><wbr>Bundle bundle = intent.getExtras();<br><wbr><wbr><wbr><wbr><wbr>int oldInt = bundle.getInt("<span style="color:#ed1c24;">previous_wifi_state</span>");<br><wbr><wbr><wbr><wbr><wbr>int newInt = bundle.getInt("<span style="color:#ed1c24;">wifi_state</span>");<br><wbr><wbr><wbr><wbr><wbr>String oldStr = (oldInt>=0 && oldInt<WIFI_STATES.length) ?WIFI_STATES[oldInt] :"?";<br><wbr><wbr><wbr><wbr><wbr>String newStr = (newInt>=0 && oldInt<WIFI_STATES.length) ?WIFI_STATES[newInt] :"?";<br><wbr><wbr><wbr><wbr><wbr>Log.e(TAG, "oldS="+oldStr+", newS="+newStr);<br><wbr><wbr><wbr><wbr><wbr>if(newInt==WifiManager.WIFI_STATE_DISABLED || newInt==WifiManager.WIFI_STATE_ENABLED) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> onWifiStateChange();<wbr> //<wbr>define this function elsewhere!<br><wbr><wbr><wbr><wbr><wbr>} else if(newInt==WifiManager.WIFI_STATE_DISABLING ||</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> newInt==WifiManager.WIFI_STATE_ENABLING)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr>chkbox_wifi.setText(newStr);<br><wbr><wbr><wbr><wbr><wbr>} else {<br><wbr><wbr><wbr><wbr><wbr><wbr>newStr += " (Is wpa_supplicant.conf readable?)";<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>chkbox_wifi.setText(newStr);<br><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr>}<br>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
// 显示Wifi状态以及ip地址:
public static String StringizeIp(int ip) {
<wbr><wbr>int ip4 = (ip>>24) & 0x000000FF;<br><wbr><wbr>int ip3 = (ip>>16) & 0x000000FF;<br><wbr><wbr>int ip2 = (ip>> 8 )& 0x000000FF;<br><wbr><wbr>int ip1 = ip<wbr><wbr><wbr><wbr><wbr><wbr> & 0x000000FF;<br><wbr><wbr>return Integer.toString(ip1) + "." + ip2 + "." + ip3 + "." + ip4;<br>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
private void onWifiStateChange() {
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> String ip_str = "";</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>WifiInfo info = mMainWifi.getConnectionInfo();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>if(info != null) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int ipaddr = info.getIpAddress();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>ip_str = " (ip="+StringizeIp(ipaddr)+")";<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr> if(mMainWifi.isWifiEnabled()==true)</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> chkbox_wifi.setText("Wifi is on [" + ip_str + "]");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr> else</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> chkbox_wifi.setText("Wifi is off");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
}
<wbr></wbr>
很多google出来的代码并未提及wifi的五个状态,实际上这在文档中说得很清楚。如果以为只有2个状态,那么,不仅状态监视会出问题,而且可能在监视到状态变化时作出错误的行为(你的onReceive()函数是否类似于多调用了几次?)。