Note: To register the right to turn WiFi on and off first,
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.wyl.wifi "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk and roid:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbackup=" true " android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" &G T <activity android:name= "com.wyl.wifi.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <cate Gory android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> & lt;/application> <!--permission to register WIFI--><uses-permission android:name= "Android.permission.ACCESS_WIFI_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.CHANGE_NETWORK_STATE" ></uses-permission></manifest >
Mainactivity.java
Package Com.wyl.wifi;import Android.content.context;import Android.database.sqlite.sqliteopenhelper;import Android.net.wifi.wifimanager;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;import Android.widget.toast;public class MainActivity Extends Actionbaractivity {Button btn; Button btn2; Button Btn3; TextView TV; EditText et; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); btn = (Button) Findviewbyid (r.id.btn01);//check NIC btn2 = (button) Findviewbyid ( R.ID.BTN02)///turn on Nic Btn3 = (Button) Findviewbyid (R.ID.BTN03);//Turn off NIC et = (EditText) Findviewbyid (R.ID.EDITTEXT2); Startwifilistener start = new Startwifilistener (); Closewifilistener close = new Closewifilistener (); Btn2.setonclicklistener (start); Btn3.setonclicklistener (close);} Class StaRtwifilistener implements onclicklistener{@Overridepublic void OnClick (View v) {//The most important thing is to get to Wifimanagerwifimanager first Wifiman = (Wifimanager) MainActivity.this.getSystemService (Context.wifi_service); wifiman.setwifienabled (true);// Turn on WiFiSystem.out.println ("WiFi State--->" +wifiman.getwifistate ()); Toast.maketext (Mainactivity.this, "" +wifiman.getwifistate (), Toast.length_long);}} Class Closewifilistener implements onclicklistener{@Overridepublic void OnClick (View v) {Wifimanager Wifiman = ( Wifimanager) MainActivity.this.getSystemService (Context.wifi_service); wifiman.setwifienabled (false);// Close WiFiSystem.out.println ("WiFi State--->" +wifiman.getwifistate ()); Toast.maketext (Mainactivity.this, "" +wifiman.getwifistate (), Toast.length_long);}}}
Android WiFi on and off