The following statuses are available for wifi:
WIFI_STATE_DISABLED = 1, Disabled
WIFI_STATE_DISABLING = 0. Shutting Down...
WIFI_STATE_ENABLED = 3, enabled
WIFI_STATE_ENABLING = 2, opening
WIFI_STATE_UNKNOWN = 4 Unknown Status
Why are there ongoing operations? It takes time to enable or disable a device.
The operation on wifi is to operate the WifiManager class. to operate on Wifi, we must first obtain the Wif service.
WifiManager wifi = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE );
The complete example is as follows:
Package com. example. wifi; import android.net. wifi. wifiManager; import android. OS. bundle; import android. app. activity; import android. content. context; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast;/*** enable or disable Wifi as long as it is a WifiManager class operation * @ author Administrator **/public class WifiActivity extends Activity {private Button open = null; private Button close = null; private Button get = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_wifi); open = (Button) findViewById (R. id. open); close = (Button) findViewById (R. id. close); get = (Button) findViewById (R. id. get); // open wifiopen. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubWifiManager wifi = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE); wifi. setWifiEnabled (true); System. out. println ("wifi state ------>" + wifi. getWifiState (); Toast. makeText (WifiActivity. this, "enable wifi state =" + wifi. getWifiState (), Toast. LENGTH_SHORT ). show () ;}}); // disable wificlose. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubWifiManager wifi = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE); wifi. setWifiEnabled (false); System. out. println ("wifi state ------>" + wifi. getWifiState (); Toast. makeText (WifiActivity. this, "Disable wifi state =" + wifi. getWifiState (), Toast. LENGTH_SHORT ). show () ;}}); // get the wifi status get. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubWifiManager wifi = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE); System. out. println ("wifi state ------>" + wifi. getWifiState (); Toast. makeText (WifiActivity. this, "get wifi state =" + wifi. getWifiState (), Toast. LENGTH_SHORT ). show ();}});}}
Closing
Sample Code
Click Open Link