WiFi NIC Status
Wifi_state_disabled WiFi NIC is not available
Wifi_state_diabling WiFi NIC is shutting down
Wifi_state_enabled WiFi NIC Available
Wifi_state_enabling WiFi Card is open
Wifi_state_unknown Unknown NIC Status
Permissions required to operate the WiFi network card
Change_network_state Changing network connection Status
Change_wifi_state changing the WiFi connection status
Access_network_state allow the app to get network status information
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