我希望在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();
崩溃了什么问题?
答案 0 :(得分:1)
您尚未初始化WifiManager
对象。以这种方式在onReceive方法中做到这一点
mainWifiObj = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);