http://alex-yang-xiansoftware-com.iteye.com/blog/619841
In some programs, you need to download data from the Internet, or other ways to generate traffic to the network, when WiFi is not available should be prompted to the user WiFi is not available, whether to continue, because if the WiFi dropped, then the program may use 3G card or other toll channels using the network, will lead to a lot of internet costs when you don't know. By looking at the Android API, you can use the following methods to determine:
- Public Static boolean iswifiactive (Context incontext) {
- Context context = Incontext.getapplicationcontext ();
- Wifimanager Wifimanager = (wifimanager) context
- . Getsystemservice (Context.wifi_service);
- return wifimanager.iswifienabled ();
- }
When using this method on the simulator, it is possible to correctly determine whether WiFi is available, but it cannot be judged on the real machine. WiFi is disconnected, but the returned result is true, resulting in inaccurate wifi judgment. The following methods can be used to determine the correct:
- Public Static boolean iswifiactive (Context incontext) {
- Context context = Incontext.getapplicationcontext ();
- Connectivitymanager connectivity = (Connectivitymanager) context
- . Getsystemservice (Context.connectivity_service);
- if (connectivity! = null) {
- Networkinfo[] info = Connectivity.getallnetworkinfo ();
- if (info! = null) {
- For (int i = 0; i < info.length; i++) {
- if (Info[i].gettypename (). Equals ("WIFI") && info[i].isconnected ()) {
- return true;
- }
- }
- }
- }
- return false;
- }
Android is a reliable way to determine whether WiFi is available, whether Android is networked