1, the status of WiFi network card
The status information for the WiFi NIC is stored in the Android.net.wifi.WifiManager class as an integer variable, with the following states:
Wifi_state_disabled
WiFi NIC not available
Wifi_state_disabling
WiFi NIC is shutting down
Wifi_state_enabled
WiFi network card available
Wifi_state_enabling
WiFi Card is open
Wifi_state_unknown
WiFi NIC Status unknown
2, operation of the WiFi network card required permissions
Change_network_state
Allow applications to change network connection status
Change_wifi_state
Allow applications to change WiFi connection status
Access_network_state
Allow applications to get state information for the network
Access_wifi_state
Allow applications to get WiFi status information
3, change the status of WiFi network card
The operation of the WiFi card requires a Wifimanager object, and the method of obtaining the object is as follows:
Copy Code code as follows:
Wifimanager wifimanager= (Wifimanager) Context.getsystemservice (Service.wifi_service);
Open the WiFi network card:
Copy Code code as follows:
Wifimanager.setwifienabled (TRUE);
Turn off the WiFi network card
Copy Code code as follows:
Wifimanager.setwifienabled (FALSE);
Get the current status of the NIC:
Copy Code code as follows:
Wifimanager.getwifistate ();
Example code: Because the Android emulator does not support WiFi operations, the example needs to run on the physical phone
Androidmanifest.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Me.bym" android:versioncode= "1" android:versionname= "1.0" >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". Wifiactivity "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>
<USES-SDK android:minsdkversion= "4"/>
<!--The following are the permissions required to access the network using WiFi-->
<uses-permission android:name= "Android.permission.CHANGE_NETWORK_STATE" ></uses-permission>
<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" ></uses-permission>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></uses-permission>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>
</manifest>
Activity
Copy Code code as follows:
Package me.bym;
Import android.app.Activity;
Import Android.content.Context;
Import Android.net.wifi.WifiManager;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Wifiactivity extends activity {
Private Button Startwifibutton = null;
Private Button Shutdownwifibutton = null;
Private Button Checkstatebutton = null;
Private Wifimanager Wifimanager = null;
Private String res = ""; A string that holds the information of the operation result
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Startwifibutton = (Button) Findviewbyid (R.id.startwifibutton);
Shutdownwifibutton = (Button) Findviewbyid (R.id.shutdownwifibutton);
Checkstatebutton = (Button) Findviewbyid (R.id.checkstatebutton);
Startwifibutton.setonclicklistener (New Startwifilistener ());
Shutdownwifibutton.setonclicklistener (New Shutdownwifilistener ());
Checkstatebutton.setonclicklistener (New Checkstatelistener ());
}
Class Startwifilistener implements Onclicklistener {
@Override
public void OnClick (View v) {
Wifimanager = (Wifimanager) wifiactivity.this
. Getsystemservice (Context.wifi_service);
if (wifimanager.setwifienabled (true)) {
res = "Start WiFi service successful";
} else {
res = "Start WiFi service failed";
}
Toast.maketext (Wifiactivity.this, Res, toast.length_short). Show ();
}
}
Class Shutdownwifilistener implements Onclicklistener {
@Override
public void OnClick (View v) {
Wifimanager = (Wifimanager) wifiactivity.this
. Getsystemservice (Context.wifi_service);
if (wifimanager.setwifienabled (false)) {
res = "Turn off WiFi service success";
} else {
res = "Shutdown WiFi service failed";
}
Toast.maketext (Wifiactivity.this, Res, toast.length_short). Show ();
}
}
Class Checkstatelistener implements Onclicklistener {
@Override
public void OnClick (View v) {
Wifimanager = (Wifimanager) wifiactivity.this
. Getsystemservice (Context.wifi_service);
Switch (wifimanager.getwifistate ()) {
Case wifimanager.wifi_state_disabled:
res = "WiFi is Off";
Break
Case wifimanager.wifi_state_disabling:
res = "WiFi is shutting in";
Break
Case wifimanager.wifi_state_enabled:
res = "WiFi enabled";
Break
Case wifimanager.wifi_state_enabling:
res = "WiFi is starting";
Break
Case Wifimanager.wifi_state_unknown:
res = "Unknown WiFi state";
Break
}
Toast.maketext (Wifiactivity.this, Res, toast.length_short). Show ();
}
}
}