[在Android上启用或禁用Wifi时如何监听和接收事件?
请注意,我不是要连接到Internet。我只是在寻找一个事件,该事件显示wifi设置中的wifi已启用。
使用WIFI_STATE_CHANGED_ACTION广播意图动作检查Wi-Fi状态。
将此添加到清单文件:
<receiver
android:name=".WifiReceiver" >
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
并使用此类接收广播:
public class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Check the state here
int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
}
}