Turn off the wifi service-wifimanager

Source: Internet
Author: User
(To http://www.cmd100.com/bbs/forum-redirect-tid-8084-goto-lastpost.html)
------------------------------------------------
Public class EX05_17 extends Activity
{
Private TextView mTextView01;
Private CheckBox mCheckBox01;

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 );

MWiFiManager01 = (WifiManager) this. getSystemService (Context. WIFI_SERVICE );

If (mWiFiManager01.isWifiEnabled ())
{
If (mWiFiManager01.getWifiState () = WifiManager. WIFI_STATE_ENABLED)
{
MCheckBox01.setChecked (true );
MCheckBox01.setText (R. string. str_uncheck );
}
Else
{
MCheckBox01.setChecked (false );
MCheckBox01.setText (R. string. str_checked );
}
}
Else
{
MCheckBox01.setChecked (false );
MCheckBox01.setText (R. string. str_checked );
}

MCheckBox01.setOnClickListener (new CheckBox. OnClickListener ()
{
@ Override
Public void onClick (View v)
{
// TODO Auto-generated method stub

If (mCheckBox01.isChecked () = false)
{
Try
{
If (mWiFiManager01.isWifiEnabled ())
{
If (mWiFiManager01.setWifiEnabled (false ))
{
MTextView01.setText (R. string. str_stop_wifi_done );
}
Else
{
MTextView01.setText (R. string. str_stop_wifi_failed );
}
}
Else
{
Switch (mWiFiManager01.getWifiState ())
{
Case WifiManager. WIFI_STATE_ENABLING:
MTextView01.setText
(
GetResources (). getText (R. string. str_stop_wifi_failed) + ":" +
GetResources (). getText (R. string. str_wifi_enabling)
);
Break;
Case WifiManager. WIFI_STATE_DISABLING:
MTextView01.setText
(
GetResources (). getText (R. string. str_stop_wifi_failed) + ":" +
GetResources (). getText (R. string. str_wifi_disabling)
);
Break;
Case WifiManager. WIFI_STATE_DISABLED:
MTextView01.setText
(
GetResources (). getText (R. string. str_stop_wifi_failed) + ":" +
GetResources (). getText (R. string. str_wifi_disabled)
);
Break;
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
{
If (! MWiFiManager01.isWifiEnabled () & mWiFiManager01.getWifiState ()! = WifiManager. WIFI_STATE_ENABLING)
{
If (mWiFiManager01.setWifiEnabled (true ))
{
Switch (mWiFiManager01.getWifiState ())
{
Case WifiManager. WIFI_STATE_ENABLING:
MTextView01.setText
(
GetResources (). getText (R. string. str_wifi_enabling)
);
Break;
Case WifiManager. WIFI_STATE_ENABLED:
MTextView01.setText
(
GetResources (). getText (R. string. str_start_wifi_done)
);
Break;
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 ())
{
Case WifiManager. WIFI_STATE_ENABLING:
MTextView01.setText
(
GetResources (). getText (R. string. str_start_wifi_failed) + ":" +
GetResources (). getText (R. string. str_wifi_enabling)
);
Break;
Case WifiManager. WIFI_STATE_DISABLING:
MTextView01.setText
(
GetResources (). getText (R. string. str_start_wifi_failed) + ":" +
GetResources (). getText (R. string. str_wifi_disabling)
);
Break;
Case WifiManager. WIFI_STATE_DISABLED:
MTextView01.setText
(
GetResources (). getText (R. string. str_start_wifi_failed) + ":" +
GetResources (). getText (R. string. str_wifi_disabled)
);
Break;
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 (EX05_17.this, str, Toast. LENGTH_LONG). show ();
}
Else
{
Toast. makeText (EX05_17.this, str, Toast. LENGTH_SHORT). show ();
}
}

@ Override
Protected void onResume ()
{
// TODO Auto-generated method stub

Try
{
Switch (mWiFiManager01.getWifiState ())
{
Case WifiManager. WIFI_STATE_ENABLED:
MTextView01.setText
(
GetResources (). getText (R. string. str_wifi_enabling)
);
Break;
Case WifiManager. WIFI_STATE_ENABLING:
MTextView01.setText
(
GetResources (). getText (R. string. str_wifi_enabling)
);
Break;
Case WifiManager. WIFI_STATE_DISABLING:
MTextView01.setText
(
GetResources (). getText (R. string. str_wifi_disabling)
);
Break;
Case WifiManager. WIFI_STATE_DISABLED:
MTextView01.setText
(
GetResources (). getText (R. string. str_wifi_disabled)
);
Break;
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 ();
}
}
--------------------------------------------
Strings. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, </string>
<String name = "app_name"> 0000100 </string>
<String name = "str_checked"> enable WiFi </string>
<String name = "str_uncheck"> disable WiFi </string>
<String name = "str_start_wifi_failed"> An error occurred while trying to enable the WiFi service. </string>
<String name = "str_start_wifi_done"> the WiFi service is successfully enabled. </string>
<String name = "str_stop_wifi_failed"> An error occurred while trying to disable the Wi-Fi service. </string>
<String name = "str_stop_wifi_done"> the WiFi service is successfully disabled. </string>
<String name = "str_wifi_enabling"> WiFi starts... </string>
<String name = "str_wifi_disabling"> when WiFi is disabled... </string>
<String name = "str_wifi_disabled"> WiFi disabled </string>
<String name = "str_wifi_unknow"> unknown WiFi status... </string>
</Resources> permission <uses-permission android: name = "android. permission. CHANGE_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/>
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. WAKE_LOCK"/>

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.