The Wi-Fi status of the Android system can be obtained through the wifimanager class. There are five statuses in total:
Wifimanager. wifi_state_disabling is stopping
Wifimanager. wifi_state_disabled stopped
Wifimanager. wifi_state_enabling is enabling
Wifimanager. wifi_state_enabled Enabled
Wifimanager. wifi_state_unknown unknown
However, the status information obtained from some devices may not be accurate, which may be related to many factors. Here we will post a simple WiFi status broadcast, in the form of messages, the two statuses of the Wi-Fi.
Code:
Public class wifistatusbroadcast extends broadcastreceiver {
Private Static final string tag = "wifistatusbroadcast ";
Private Static string opened = NULL;
Private Static string closed = NULL;
Public static Boolean wifi_enabled = false;
Public void onreceive (context, intent ){
Log. D (TAG, "onreceive:" + intent. getaction ());
Log. D (TAG, "In action_boot_completed ");
Opened = context. getresources (). getstring (R. String. wifi_opened );
Log. D (TAG, "wifistatus" + opened );
Closed = context. getresources (). getstring (R. String. wifi_closed );
Log. D (TAG, "wifistatus" + closed );
Connectivitymanager = (connectivitymanager) Context. getsystemservice (context. connectivity_service );
Final networkinfo activenetinfo = (networkinfo) intent. getparcelableextra (wifimanager. extra_network_info );
Log. D (TAG, "activenetinfo is" + activenetinfo );
If (activenetinfo! = NULL & activenetinfo. isconnected ())
{
If (wifi_enabled = false)
{
Toast. maketext (context, opened, 2000). Show ();
Wifi_enabled = true;
}
Log. D (TAG, "WiFi was connected ");
} Else {
If (wifi_enabled = true)
{
Toast. maketext (context, closed, 5000). Show ();
Wifi_enabled = false;
}
Log. D (TAG, "WiFi was disconnected ");
}
}
}
You can write a system APK or compile it under the framework. When the WiFi status changes, the broadcast will be sent, and this class can receive and handle it.