Enable and disable WiFi

Source: Internet
Author: User

1. WiFi Nic status

There are five major statuses:

Wifi_state_disabled: WiFi Nic unavailable

Wifi_state_disabling: WiFi is being disabled

Wifi_state_enabled: WiFi Nic available

Wifi_state_enabled: the WiFi Nic is on.

Wifi_state_unknown: Unknown Nic status

2. WiFi is managed mainly through the wifimanager class. The procedure is as follows:

(1) first enable WiFi-related Permissions

 <  Uses-Permission  Android: Name  = "Android. Permission. access_network_state"  />  <  Uses-Permission Android: Name  = "Android. Permission. access_wifi_state"  />  <  Uses-Permission  Android: Name  = "Android. Permission. change_network_state"  />  <  Uses-Permission  Android: Name  = "Android. Permission. change_wifi_state"  /> 

(2) obtain the wifimanager object in the context object

Wifimanager = (wifimanager) getsystemservice (wifi_service );

(3) Enable, disable, and query the WiFi status through the change WiFi status method in the wifimanager class

Wifimanager. setwifienabled (true); // the WiFi status changes from wifi_state_enabled to wifi_state_enabled;

Wifimanager. setwifienabled (false); // The status of wifi disabling changes from wifi_state_disabling to wifi_state_disabled;

Wifimanager. getwifistate (); // query the WiFi status

3.Source codeAs follows:

(1) The androidmanifest. xml file is as follows:

 <?  XML version = "1.0" encoding = "UTF-8" ?>  <  Manifest  Xmlns: Android  = "Http://schemas.android.com/apk/res/android"  Package  = "Com. haitun. WiFi"  Android: versioncode  = "1"  Android: versionname  = "1.0"   >      <  Uses-SDK  Android: minsdkversion = "8"  Android: targetsdkversion  = "8"   />      <  Application  Android: allowbackup  = "True"  Android: icon  = "@ Drawable/ic_launcher"  Android: Label  = "@ String/app_name"  Android: Theme  = "@ Style/apptheme"   >         <  Activity  Android: Name  = "Com. haitun. Wifi. mainactivity"  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-Permission Android: Name  = "Android. Permission. access_network_state"  />      <  Uses-Permission  Android: Name  = "Android. Permission. access_wifi_state"  />      <  Uses-Permission  Android: Name  = "Android. Permission. change_network_state"  />      <  Uses-Permission Android: Name  = "Android. Permission. change_wifi_state"  />  </  Manifest  > 

(2) JAVA SourceCode

 Package  Com. haitun. WiFi;  Import  Android.net. Wifi. wifimanager;  Import  Android. OS. Bundle;  Import  Android. App. activity; Import  Android. View. Menu;  Import  Android. View. menuitem;  Import  Android. widget. Toast;  Public   Class Mainactivity Extends  Activity {  Private   Final   Static   Int Start = 1 ; Private   Final   Static   Int Stop = 2 ;  Private   Final   Static   Int Check = 3 ;  Private Wifimanager = Null  ; @ Override  Protected   Void Oncreate (bundle savedinstancestate ){  Super  . Oncreate (savedinstancestate); setcontentview (R. layout. Main) ;}@ override  Public   Boolean  Oncreateoptionsmenu (menu ){  //  Inflate the menu; this adds items to the action bar if it is present.  //  Add menu button Menu. Add (0, start, 1, "enable WiFi" ); Menu. Add ( 0, stop, 2, "Disable WiFi"); Menu. Add ( 0, check, 3, "Check WiFi status" );  Return   True  ;} @ Override  Public   Boolean  Onoptionsitemselected (menuitem item ){  //  Todo auto-generated method stub  //  Enable WiFi          If (Item. getitemid () = Start) {wifimanager = (Wifimanager) getsystemservice (wifi_service); wifimanager. setwifienabled (  True  ); Toast. maketext (  This , "The current wireless network adapter status is:" + wifimanager. getwifistate () + "~ " , Toast. length_short). Show ();}  //  Disable WiFi          Else   If (Item. getitemid () = Stop) {wifimanager = (Wifimanager) getsystemservice (wifi_service); wifimanager. setwifienabled ( False  ); Toast. maketext (  This , "The current wireless network adapter status is:" + wifimanager. getwifistate () + "~ " , Toast. length_short). Show ();}  //  Query WiFi          Else   If (Item. getitemid () = Check ){  Int  State; State = Wifimanager. getwifistate (); toast. maketext (  This , "The current wireless Nic status is:" + state + "~ " , Toast. length_short). Show ();}  Return   Super  . Onoptionsitemselected (item );}} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.