A simple WiFi status prompt Broadcast

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.