Wifi Nic status
1. WIFI_STATE_DISABLED: WIFI Nic unavailable
2. WIFI_STATE_DISABLING: the WIFI Nic is being disabled.
1. WIFI_STATE_EWNABLED: WIFI Nic available
1. WIFI_STATE_ENABLING: the WIFI Nic is on.
1. WIFI_STATE_UNKNOWN: Unknown Nic status
Change the status of the Wi-Fi network card
You need to use the WifiManager object to perform operations on the Wi-Fi network card. The method to obtain this object is as follows:
WifiManger wifiManger = (WifiManger) Context. getSystemService (Service. WIFI_SERVICE); // obtain the System Service
Enable WIFI Nic
WifiManger. setWifiEnabled (true );
Enable WIFI Nic
WifiManger. setWifiEnabled (false );
Obtain the current Nic status
WifiManger. getWifiState ();
Android must grant app permissions before operating on wifi:
Add red code to the AndroidManifest. xml file
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. yx. wificontrol"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "18"/>
<Application
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Activity
Android: name = "com. yx. wificontrol. WifiControlActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
<! -- Add permissions required for the Wi-Fi network -->
<Uses-permission android: name = "android. permission. CHANGE_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/>
</Manifest>
The code is relatively simple. The WifiControlActivity. java File
Package com. yx. wificontrol;
Import android.net. wifi. WifiManager;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. content. Context;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;
Public class WifiControlActivity extends Activity {
Private Button startWifi;
Private Button stopWifi;
Private Button seeWifi;
Private WifiManager wifiManager;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_wifi_control );
StartWifi = (Button) findViewById (R. id. startWifi );
StopWifi = (Button) findViewById (R. id. stopWifi );
SeeWifi = (Button) findViewById (R. id. seeWifi );
StartWifi. setOnClickListener (new StartWifiListener ());
StopWifi. setOnClickListener (new StopWifiListener ());
SeeWifi. setOnClickListener (new SeeWifiListener ());
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. wifi_control, menu );
Return true;
}
Class StartWifiListener implements OnClickListener {
@ Override
Public void onClick (View v ){
System. out. println ("StartWifiListener ");
// Toast. makeText (WifiControlActivity. this, "Current Nic status:" + wifiManager. getWifiState (), Toast. LENGTH_LONG );
WifiManager = (WifiManager) WifiControlActivity. this. getSystemService (Context. WIFI_SERVICE );
WifiManager. setWifiEnabled (true );
System. out. println ("wifi status:" + wifiManager. getWifiState ());
Toast. makeText (WifiControlActivity. this, "Current Nic status:" + wifiManager. getWifiState (), Toast. LENGTH_LONG). show ();
}
}
Class StopWifiListener implements OnClickListener {
@ Override
Public void onClick (View v ){
System. out. println ("StopWifiListener ");
// Toast. makeText (WifiControlActivity. this, "Current Nic status:" + wifiManager. getWifiState (), Toast. LENGTH_SHORT );
WifiManager = (WifiManager) WifiControlActivity. this. getSystemService (Context. WIFI_SERVICE );
WifiManager. setWifiEnabled (false );
System. out. println ("wifi status:" + wifiManager. getWifiState ());
Toast. makeText (WifiControlActivity. this, "Current Nic status:" + wifiManager. getWifiState (), Toast. LENGTH_SHORT). show ();
}
}
Class SeeWifiListener implements OnClickListener {
@ Override
Public void onClick (View v ){
System. out. println ("SeeWifiListener ");
WifiManager = (WifiManager) WifiControlActivity. this. getSystemService (Context. WIFI_SERVICE );
System. out. println ("wifi status:" + wifiManager. getWifiState ());
Toast. makeText (WifiControlActivity. this, "Current Nic status:" + wifiManager. getWifiState (), Toast. LENGTH_LONG). show ();
}
}
}
By the way, paste the layout File
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". WifiControlActivity">
<Button
Android: id = "@ + id/startWifi"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "enable wifi nic"/>
<Button
Android: id = "@ + id/stopWifi"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/startWifi"
Android: text = "Disable wifi nic"/>
<Button
Android: id = "@ + id/seeWifi"
Android: layout_below = "@ id/stopWifi"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "viewing wifi Nic status"/>
</RelativeLayout>