在android.net.wifi.STATE_CHANGE检测到wifi状态时崩溃

时间:2015-03-16 13:29:32

标签: android wifi intentfilter

我希望在wifi状态发生变化时获得the wifi state,如下所示:

清单:

<intent-filter>
    <action android:name="android.net.wifi.STATE_CHANGE" />          
</intent-filter>

服务

WifiManager mainWifiObj;

    @Override
    public void onReceive(Context context, Intent intent) {
        WifiInfo wifiInfo = mainWifiObj.getConnectionInfo();
        String WifiState = "";
        if (mainWifiObj.isWifiEnabled()) {
            WifiState = "Wifi On";
        } else {
            WifiState = "Wifi Off";
        }
    }

但它在WifiInfo wifiInfo = mainWifiObj.getConnectionInfo();崩溃了什么问题?

1 个答案:

答案 0 :(得分:1)

您尚未初始化WifiManager对象。以这种方式在onReceive方法中做到这一点

mainWifiObj = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);