Android Learning Note-wifi Network operation

Source: Internet
Author: User

WiFi NIC Status

    1. Wifi_state_disabled WiFi NIC is not available

    2. Wifi_state_diabling WiFi NIC is shutting down

    3. Wifi_state_enabled WiFi NIC Available

    4. Wifi_state_enabling WiFi Card is open

    5. Wifi_state_unknown Unknown NIC Status


Permissions required to operate the WiFi network card

    1. Change_network_state Changing network connection Status

    2. Change_wifi_state changing the WiFi connection status

    3. Access_network_state allow the app to get network status information

    4. Access_wifi_state allow app to get WIFI status information


Interface Activity_main.xml

 <textview        android:id= "@+id/hello"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android:text= "@ String/hello_world " />        <button android:id=" @+id /startwifi "    android:layout_width=" Fill_parent "     android: layout_height= "Wrap_content"      android:layout_below= "@id/hello"      android:text= "Start your phone's WiFi card"     /><button android:id= "@+id/stopWifi"      android:layout_width= "Fill_parent"      android:layout_height= "Wrap_ Content "     android:layout_below=" @id/startwifi "    android:text = "Turn off your phone's WiFi card"   &Nbsp; /><button android:id= "@+id/checkwifi"     android:layout_width= "Fill_ Parent "     android:layout_height=" Wrap_content "     android : layout_below= "@id/stopwifi"     android:text= "Check current WiFi card status"     />

The permissions Required

<!--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>


Mainactivity.java

Public class mainactivity extends activity {private button startbutton  = null;private button stopbutton = null;private button checkbutton  = null;private WifiManager wifiManager = null;     @Override     public void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (r.layout.activity_main);         startbutton  =  (Button) Findviewbyid (R.id.startwifi);         stopbutton  =  (Button) Findviewbyid (R.id.stopwifi);         checkbutton  =  (Button) Findviewbyid (R.id.checkwifi);         Startbutton.setonclicklistener (new stArtwifilistener ());         stopbutton.setonclicklistener (new  Stopwifilistener ());         checkbutton.setonclicklistener (new  Checkwifilistener ());    }    class startwifilistener  implements onclicklistener{@Overridepublic  void onclick (view v)  {wifimanager =   (Wifimanager) MainActivity.this.getSystemService (Context.wifi_service); wifimanager.setwifienabled (true); System.out.println ("wifi state --->"  + wifimanager.getwifistate ()); Toast.maketext (mainactivity.this,  "Current WiFi card status is"  + wifimanager.getwifistate (),  Toast.LENGTH _short). Show ();}     }    class StopWifiListener implements  onclicklistener{@Overridepublic  void onclick (view arg0)  {// todo auto-generated  method stubwifimanager =&nBSP; (Wifimanager) MainActivity.this.getSystemService (Context.wifi_service); wifimanager.setwifienabled (false); System.out.println ("wifi state --->"  + wifimanager.getwifistate ());/* Getwifistate returns one of the following five constants the following five constants are predefined in the Wifimanager class Public static final int wifi_state_ Disabling = 0;    public static final int wifi_state_ Disabled = 1;    public static final int wifi_state_enabling  = 2;    public static final int wifi_state_enabled =  3;    public static final int WIFI_STATE_UNKNOWN = 4;  */toast.maketext (mainactivity.this,  "Current WiFi card status is"  + wifimanager.getwifistate (),  Toast.length_short). Show ();}     }        class CheckWifiListener  Implements onclicklistener{@Overridepublic  void onclick (view v)  {wifiManager =  (Wifimanager) MainActivity.this.getSystemService (Context.wifi_service); System.out.println ("wifi state --->"  + wifimanager.getwifistate ()); Toast.maketext (mainactivity.this,  "Current WiFi card status is"  + wifimanager.getwifistate (),  Toast.LENGTH _short). Show ();}         }}


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/53/F9/wKioL1R1RyiSLiePAAZ8x3KUHlk955.jpg "style=" float: none; "title=" Screenshot_2014-11-26-11-00-55.png "alt=" Wkiol1r1ryisliepaaz8x3kuhlk955.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/53/FB/wKiom1R1Rqjy_JICAAG0Y3cUCXE990.jpg "style=" float: none; "title=" Screenshot_2014-11-26-11-01-53.png "alt=" Wkiom1r1rqjy_jicaag0y3cucxe990.jpg "/>




This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1582680

Android Learning Note-wifi Network operation

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.