Android study note 09. WiFi network operations

Source: Internet
Author: User

1. Concepts

2. WiFi Nic status

Wifi_state_disabled: WiFi Nic unavailable (1)

Wifi_state_disabling: WiFi is being disabled (0)

Wifi_state_enabled: WiFi Nic available (3)

Wifi_state_enabling: the WiFi Nic is on (2)

Wifi_state_unknown: Unknown Nic status

3. WiFi permission

(Some network-related permissions. For other permissions, see the help documentation)

4. Change the status of the Wi-Fi network card

To operate a Wi-Fi network card, you need to perform the wifimanager object as follows:

Wifimanager = (wifimanager) Context. getsystemservice (service. wifi_service );

Enable the NIC: wifimanager. setwifienabled (true );

Disable the WiFi NIC: wifimanager. setwifienabled (false );

Obtain the current Nic status: wifimanager. getwifistate ();

Now a Wi-Fi access is implementedProgramYou must have an Android mobile phone for testing. The specific content is to start, stop, and view the Wi-Fi network.

Wifiactivity. Java

 1   Package  Zzl. WiFi;  2   3   Import  Android. App. activity;  4   Import  Android. content. context; 5   Import  Android.net. Wifi. wifimanager;  6   Import  Android. OS. Bundle;  7   Import  Android. View. Menu;  8   Import  Android. View. view;  9   Import  Android. View. View. onclicklistener;  10  Import  Android. widget. Button;  11   Import  Android. widget. Toast;  12   13   Public   Class Wifiactivity Extends  Activity {  14   15       Private Button start = Null  ; 16       Private Button stop = Null  ;  17       Private Button check = Null  ;  18       Private Wifimanager = Null  ;  19   @ Override  20       Protected  Void  Oncreate (bundle savedinstancestate ){  21           Super  . Oncreate (savedinstancestate );  22   Setcontentview (R. layout. Main );  23           24 Start = (Button) findviewbyid (R. Id. Start );  25 Stop = (Button) findviewbyid (R. Id. Stop );  26 Check =(Button) findviewbyid (R. Id. Check );  27           28 Start. setonclicklistener ( New  Startwifilistener ());  29 Stop. setonclicklistener ( New  Stopwifilistener ());  30 Check. setonclicklistener ( New  Checkwifilistener ());  31   }  32  33       Class Startwifilistener Implements  Onclicklistener {  34   35   @ Override  36           Public   Void  Onclick (view v ){  37               //  Todo auto-generated method stub  38               // Wifiactivity inherits from activity,  39               //  Activity itself is a subclass of context (so all functions of context can be used by activity)  40 Wifimanager = (wifimanager) wifiactivity. This  . Getsystemservice (context. wifi_service );  41 Wifimanager. setwifienabled ( True  );  42 System. Out. println ("wiif State --->" + Wifimanager. getwifistate ()); 43 Toast. maketext (wifiactivity. This , "Current WiFi status:" + Wifimanager. getwifistate (), Toast. length_short). Show ();  44   }  45   }  46       Class Stopwifilistener Implements  Onclicklistener {  47   48   @ Override 49           Public   Void  Onclick (view v ){  50               //  Todo auto-generated method stub  51 Wifimanager = (wifimanager) wifiactivity. This  . Getsystemservice (context. wifi_service );  52 Wifimanager. setwifienabled ( False  );  53 System. Out. println ("wiif State --->" + Wifimanager. getwifistate ());  54 Toast. maketext (wifiactivity. This , "Current WiFi status:" + Wifimanager. getwifistate (), Toast. length_short). Show ();  55   }  56   }  57       Class Checkwifilistener Implements  Onclicklistener {  58  59   @ Override  60           Public   Void  Onclick (view v ){  61               //  Todo auto-generated method stub  62 Wifimanager = (wifimanager) wifiactivity. This  . Getsystemservice (context. wifi_service );  63 System. Out. println ("wiif State --->" +Wifimanager. getwifistate ());  64 Toast. maketext (wifiactivity. This , "Current WiFi status:" + Wifimanager. getwifistate (), Toast. length_short). Show ();  65   }  66   }  67   @ Override  68       Public   Boolean  Oncreateoptionsmenu (menu ){ 69           //  Inflate the menu; this adds items to the action bar if it is present.  70   Getmenuinflater (). Inflate (R. Menu. Main, menu );  71           Return   True  ;  72   }  73   74 }

Main. xml

 1   < Linearlayout  Xmlns: Android  = "Http://schemas.android.com/apk/res/android"  2   Xmlns: Tools  = "Http://schemas.android.com/tools"  3   Android: Orientation  = "Vertical"  4   Android: layout_width  = "Match_parent"  5   Android: layout_height = "Match_parent"  6   Tools: Context  = ". Wifiactivity"   >  7   8       <  Button  9           Android: layout_width  = "Fill_parent"  10   Android: layout_height  = "Wrap_content"  11  Android: ID  = "@ + ID/start"  12   Android: Text  = "@ String/start"  />  13       <  Button  14           Android: layout_width  = "Fill_parent"  15   Android: layout_height  = "Wrap_content"  16  Android: ID  = "@ + ID/stop"  17   Android: Text  = "@ String/stop"  />  18       <  Button  19           Android: layout_width  = "Fill_parent"  20   Android: layout_height  = "Wrap_content"  21  Android: ID  = "@ + ID/check"  22   Android: Text  = "@ String/check"  />  23   24   </  Linearlayout  > 

Partial results:

Summary:

(1. The <uses-Permission> tag appears after <Application> tag warning appears in manifest. xml.

Solution:Put <uses-Permission> in front of <Application>

(2) To enable Wi-Fi access to the network, you need to write such information in manifest. xml.CodeOtherwise, the access permission may be insufficient.

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

(3. connect to your mobile phone for testing.

 

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.