2009年12月8日 星期二

Android wifi on off tutorial

Turn off, Turn on wifi in android using code tutorial

For list of android tutorial click here

Here is how to turn on and turn off wifi in android.

First you need to declare the following in your manifest file
XML | copy code | ?
1



2



3



4



After doing it that on your Activity class
Java | copy code | ?
01

private WifiManager wifiManager;

02

@Override

03

public void onCreate(Bundle icicle) {

04

....................

05

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

06

if(wifiManager.isWifiEnabled()){

07

wifiManager.setWifiEnabled(false);

08

}else{

09

wifiManager.setWifiEnabled(true);

10

}

11

}

Explanation
Get the Wifi service from our system
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

Check the our wifi is currently turned on or turned off
if(wifiManager.isWifiEnabled()){

Turn on/off our wifi
wifiManager.setWifiEnabled();

Reference
WifiEnabler
Permissions Journey: ACCESS_WIFI_STATE
Share and Enjoy:

* Digg
* Sphinn
* del.icio.us
* Facebook
* Mixx
* Google Bookmarks
* Reddit
* Yahoo! Buzz
* email
* Fark
* HackerNews
* PDF
* Slashdot
* StumbleUpon
* Technorati

October 17th, 2009 | Tags: Wifi | Category: Android | One comment
1 comment to Turn off, Turn on wifi in android using code tutorial

沒有留言:

張貼留言