Operations on Wifi for Android

Source: Internet
Author: User

The following statuses are available for wifi:

WIFI_STATE_DISABLED = 1, Disabled
WIFI_STATE_DISABLING = 0. Shutting Down...
WIFI_STATE_ENABLED = 3, enabled
WIFI_STATE_ENABLING = 2, opening
WIFI_STATE_UNKNOWN = 4 Unknown Status

Why are there ongoing operations? It takes time to enable or disable a device.

The operation on wifi is to operate the WifiManager class. to operate on Wifi, we must first obtain the Wif service.

WifiManager wifi = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE );

The complete example is as follows:

Package com. example. wifi; import android.net. wifi. wifiManager; import android. OS. bundle; import android. app. activity; import android. content. context; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast;/*** enable or disable Wifi as long as it is a WifiManager class operation * @ author Administrator **/public class WifiActivity extends Activity {private Button open = null; private Button close = null; private Button get = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_wifi); open = (Button) findViewById (R. id. open); close = (Button) findViewById (R. id. close); get = (Button) findViewById (R. id. get); // open wifiopen. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubWifiManager wifi = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE); wifi. setWifiEnabled (true); System. out. println ("wifi state ------>" + wifi. getWifiState (); Toast. makeText (WifiActivity. this, "enable wifi state =" + wifi. getWifiState (), Toast. LENGTH_SHORT ). show () ;}}); // disable wificlose. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubWifiManager wifi = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE); wifi. setWifiEnabled (false); System. out. println ("wifi state ------>" + wifi. getWifiState (); Toast. makeText (WifiActivity. this, "Disable wifi state =" + wifi. getWifiState (), Toast. LENGTH_SHORT ). show () ;}}); // get the wifi status get. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubWifiManager wifi = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE); System. out. println ("wifi state ------>" + wifi. getWifiState (); Toast. makeText (WifiActivity. this, "get wifi state =" + wifi. getWifiState (), Toast. LENGTH_SHORT ). show ();}});}}

Closing


Sample Code

Click Open Link








Related Article

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.