Android WiFi on and off

Source: Internet
Author: User

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

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.