Need to configure permissions
To Add code in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
2. Use the Connectivitymanager class to encode operations.
Public void getnetworkstate () { connectivitymanager connmanager = (Connectivitymanager) this.getsystemservice (connectivity_service); // Get Networkinfo objects that represent the networked state NetworkInfo networkInfo = Connmanager.getactivenetworkinfo (); // gets whether the current network connection is available if (Null == networkinfo) { toast.maketext ( this, "Current network connection is not available", toast.length_short). Show (); // When the network is unavailable, jump to the Network Settings page startactivityforresult (new intent ( Android.provider.Settings.ACTION_WIRELESS_SETTINGS), 1); } else { boolean available = networkinfo.isavailable (); if (available) { log.i ("Notification", " The current network connection is available "); toast.maketext (this, "Current network connection available", toast.length_short). Show (); } else {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("notification", "Current network connection is not available"); toast.maketext (this, "Current network connection is not available", toast.length_short). Show (); } } networkinfo.state state = connmanager.getnetworkinfo ( Connectivitymanager.type_mobile). GetState (); if (networkinfo.state.connected == state) { log.i ("Notification", "GPRS network is connected"); Toast.maketext (this, "GPRS network is connected", toast.length_short). Show (); } state = connmanager.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); if (networkinfo.state.connected == state) { &NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("Notification", "WiFi network is connected"); Toast.maketext (this, "WiFi network is Connected", toast.length_short). Show (); } //// Jump to wireless Network Setup interface // startactivity (new // intent (Android.provider.Settings.ACTION_WIRELESS_SETTINGS)); //// Jump to the unlimited WiFi network Settings Interface // startactivity (New // intent ( Android.provider.Settings.ACTION_WIFI_SETTINGS));}
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>
Network State Acquisition