Set the control switch for Android WiFi

Source: Internet
Author: User

Public class WiFi extends activity {private textview mtextview01; private checkbox mcheckbox01;/* Create a wifimanager object */private wifimanager mwifimanager01;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mtextview01 = (textview) findviewbyid (R. id. mytextview1); mcheckbox01 = (Checkbox) findviewbyid (R. id. mycheckbox1);/* Get wifi_service using getsystemservice */mwifimanager01 = (wifimanager) This. getsystemservice (context. wifi_service);/* determines whether the WiFi status after the program is enabled or enabled */If (mwifimanager01.iswifienabled ()) {/* determine whether the WiFi status is "enabled" */If (mwifimanager01.getwifistate () = wifimanager. wifi_state_enabled) {/* If WiFi is enabled, check the selected items */mcheckbox01.setchecked (true);/* change the selected text to disable WiFi */mcheckbox01.s Ettext (R. string. str_uncheck);} else {/* If WiFi is not enabled, select "cancel */mcheckbox01.setchecked (false)" from the check box ); /* change the selected text to enable WiFi */mcheckbox01.settext (R. string. str_checked) ;}} else {mcheckbox01.setchecked (false); mcheckbox01.settext (R. string. str_checked);}/* capture the checkbox Click Event */mcheckbox01.setonclicklistener (New checkbox. onclicklistener () {@ override public void onclick (view v) {// todo auto-generated Method Stub/* When the selected item is deselected */If (mcheckbox01.ischecked () = false) {/* try to disable the Wi-Fi Service */try {/* check whether the Wi-Fi status is enabled */If (mwifimanager01.iswifienabled ()) {/* disable WiFi */If (mwifimanager01.setwifienabled (false) {mtextview01.settext (R. string. str_stop_wifi_done);} else {mtextview01.settext (R. string. str_stop_wifi_failed);} else {/* When the WiFi status is not enabled */switch (mwifimanager01.getwifistate () {/* When the WiFi is on, resulting in Unable to close... */case wifimanager. wifi_state_enabling: mtextview01.settext (getresources (). gettext (R. string. str_stop_wifi_failed) + ":" + getresources (). gettext (R. string. str_wifi_enabling); break;/* the WiFi is being disabled, so it cannot be closed... */case wifimanager. wifi_state_disabling: mtextview01.settext (getresources (). gettext (R. string. str_stop_wifi_failed) + ":" + getresources (). gettext (R. string. str_wifi_disabling); br Eak;/* WiFi disabled */case wifimanager. wifi_state_disabled: mtextview01.settext (getresources (). gettext (R. string. str_stop_wifi_failed) + ":" + getresources (). gettext (R. string. str_wifi_disabled); break;/* unable to obtain or identify WiFi status */case wifimanager. wifi_state_unknown: Default: mtextview01.settext (getresources (). gettext (R. string. str_stop_wifi_failed) + ":" + getresources (). gettext (R. string. str_wifi_unknow) ); Break;} mcheckbox01.settext (R. string. str_checked) ;}} catch (exception e) {log. I ("Hippo", E. tostring (); E. printstacktrace () ;}} else if (mcheckbox01.ischecked () = true) {/* try to enable the Wi-Fi Service */try {/* confirm that the Wi-Fi service is disabled and is not in the open job */If (! Mwifimanager01.iswifienabled () & mwifimanager01.getwifistate ()! = Wifimanager. wifi_state_enabling) {If (mwifimanager01.setwifienabled (true) {Switch (mwifimanager01.getwifistate () {/* the WiFi is being turned on and cannot be turned on... */case wifimanager. wifi_state_enabling: mtextview01.settext (getresources (). gettext (R. string. str_wifi_enabling); break;/* WiFi is enabled and cannot be enabled again... */case wifimanager. wifi_state_enabled: mtextview01.settext (getresources (). gettext (R. string. str_start_wifi_done); break;/* other unknown errors */Default: mtextview01.settext (getresources (). gettext (R. string. str_start_wifi_failed) + ":" + getresources (). gettext (R. string. str_wifi_unknow); break ;}} else {mtextview01.settext (R. string. str_start_wifi_failed);} else {Switch (mwifimanager01.getwifistate () {/* the WiFi is being turned on, so it cannot be turned on... */case wifimanager. wifi_state_enabling: mtextview01.settext (getresources (). gettext (R. string. str_start_wifi_failed) + ":" + getresources (). gettext (R. string. str_wifi_enabling); break;/* the WiFi is being disabled, so it cannot be opened... */case wifimanager. wifi_state_disabling: mtextview01.settext (getresources (). gettext (R. string. str_start_wifi_failed) + ":" + getresources (). gettext (R. string. str_wifi_disabling); break;/* WiFi disabled */case wifimanager. wifi_state_disabled: mtextview01.settext (getresources (). gettext (R. string. str_start_wifi_failed) + ":" + getresources (). gettext (R. string. str_wifi_disabled); break;/* unable to obtain or identify WiFi status */case wifimanager. wifi_state_unknown: Default: mtextview01.settext (getresources (). gettext (R. string. str_start_wifi_failed) + ":" + getresources (). gettext (R. string. str_wifi_unknow); break ;}} mcheckbox01.settext (R. string. str_uncheck);} catch (exception e) {log. I ("Hippo", E. tostring (); E. printstacktrace () ;}}});} public void mmaketexttoast (string STR, Boolean islong) {If (islong = true) {toast. maketext (WiFi. this, STR, toast. length_long ). show ();} else {toast. maketext (WiFi. this, STR, toast. length_short ). show () ;}@ override protected void onresume () {// todo auto-generated method stub/* rewrite the event in onresume to get the current WiFi status when the program is opened */try {Switch (mwifimanager01.getwifistate ()) {/* WiFi is already on... */case wifimanager. wifi_state_enabled: mtextview01.settext (getresources (). gettext (R. string. str_wifi_enabling); break;/* the status of wifi is being enabled... */case wifimanager. wifi_state_enabling: mtextview01.settext (getresources (). gettext (R. string. str_wifi_enabling); break;/* WiFi is being disabled... */case wifimanager. wifi_state_disabling: mtextview01.settext (getresources (). gettext (R. string. str_wifi_disabling); break;/* WiFi disabled */case wifimanager. wifi_state_disabled: mtextview01.settext (getresources (). gettext (R. string. str_wifi_disabled); break;/* unable to obtain or identify WiFi status */case wifimanager. wifi_state_unknown: Default: mtextview01.settext (getresources (). gettext (R. string. str_wifi_unknow); break ;}} catch (exception e) {mtextview01.settext (E. tostring (); E. getstacktrace ();} super. onresume () ;}@ override protected void onpause () {// todo auto-generated method stub super. onpause ();}}

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "IRDC. wiFi "Android: versioncode =" 1 "Android: versionname =" 1.0.0 "> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <activity Android: Name = "IRDC. wiFi. wiFi "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. Category. launcher "/> </intent-filter> </activity> </Application> <! -- Declare permissions related to WiFi and network --> <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> <uses-Permission Android: Name =" android. permission. internet "> </uses-Permission> <uses-Permission Android: Name =" android. permission. wake_lock "> </uses-Permission> </manifest>

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:background="@drawable/white"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  >  <TextView    android:id="@+id/myTextView1"    android:layout_width="fill_parent"     android:layout_height="wrap_content"    android:textColor="@drawable/blue"    android:text="@string/hello"  />  <CheckBox    android:id="@+id/myCheckBox1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/str_checked"    android:textColor="@drawable/blue"  /></LinearLayout>

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.