Android Application Software Development (14) WiFi

Source: Internet
Author: User

Wifi is a wireless networking technology.

To use wifi to connect to the network, you must first set the permissions required to operate the WiFi network. Generally, you can obtain the following four permissions at the same time.

Change_network_state

Allows applications to change network connectivity state

Change_wifi_state

Allows applications to change wi-fi connectivity state

Access_network_state

Allows applications to access information about Networks

Access_wifi_state

Allows applications to access information about Wi-Fi networks

Obtain the status of the Wi-Fi network card
System-level service management objects such as WiFi usually need to be obtained by calling the getsystemservice () method of the context object. This method receives a string as a parameter, depending on the parameter, different system service management objects are obtained. The object for managing the Wi-Fi network card is wifimanager. The method for getting this object is as follows:
Wifimanager = (wifimanager) Context. getsystemservice (context. wifi_service );

For wifimanager, the following operations are frequently used:

Obtain the current Nic status:
Int wifistate = wifimanger. getwifistate ();

This function returns an integer. Different return values represent different States. Each State corresponds to a constant. These constants are stored in the wifimanager class. Their meanings are as follows:
Wifi_state_disabled: WiFi Nic unavailable

Wifi_state_disabling: WiFi is being disabled

Wifi_state_enabled: WiFi Nic available

Wifi_state_enabling: the WiFi Nic is on.

Wifi_state_unknown: Unknown Nic status

It may take some time to enable or disable a Wi-Fi network card. That is to say, if the network card of our mobile phone is in the available status, after we close the network card, it is not immediately closed, but is in the closing status, after the closing action is completed, it will actually enter the closed state!

Modify the current Nic status
After obtaining the wifimanager object, you can modify the status of the Wi-Fi network card as follows:
Enable WiFi Nic
Wifimanger. setwifienabled (true );
Disable WiFi Nic
Wifimanger. setwifienabled (false );

The following is a simple program code:

1. Set permissions in manifest

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "Mars. wiFi "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <activity Android: Name = ". wifiactivity "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> <uses-SDK Android: minsdkversion =" 4 "/> <! -- The following are the permissions required to access the network using WiFi --> <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> </manifest>

Set the WiFi Nic status in wifiactivity. Java

Package Mars. wiFi; import android. app. activity; import android. content. context; import android.net. wiFi. wifimanager; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. toast; public class wifiactivity extends activity {/** called when the activity is first created. */private button startbutton = NULL; private button stopbutton = NULL; private button checkbutton = NULL; private wifimanager = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); startbutton = (button) findviewbyid (R. id. startwifi); stopbutton = (button) findviewbyid (R. id. stopwifi); checkbutton = (button) findviewbyid (R. id. checkwifi); startbutton. setonclicklistener (New startwifilistener (); stopbutton. setonclicklistener (New stopwifilistener (); checkbutton. setonclicklistener (New checkwifilistener ();} class startwifilistener implements onclicklistener {@ overridepublic void onclick (view v) {wifimanager = (wifimanager) wifiactivity. this. getsystemservice (context. wifi_service); // sets the available wifimanager status. setwifienabled (true); system. out. println ("WiFi State --->" + wifimanager. getwifistate (); toast. maketext (wifiactivity. this, "the current WiFi Nic status is" + wifimanager. getwifistate (), toast. length_short ). show () ;}} class stopwifilistener implements onclicklistener {@ overridepublic void onclick (view arg0) {// todo auto-generated method stubwifimanager = (wifimanager) wifiactivity. this. getsystemservice (context. wifi_service); // sets that the status is unavailable. setwifienabled (false); system. out. println ("WiFi State --->" + wifimanager. getwifistate (); toast. maketext (wifiactivity. this, "the current WiFi Nic status is" + wifimanager. getwifistate (), toast. length_short ). show () ;}} class checkwifilistener implements onclicklistener {@ overridepublic void onclick (view v) {wifimanager = (wifimanager) wifiactivity. this. getsystemservice (context. wifi_service); system. out. println ("WiFi State --->" + wifimanager. getwifistate (); toast. maketext (wifiactivity. this, "the current WiFi Nic status is" + wifimanager. getwifistate (), toast. length_short ). show ();}}}
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.