Vous êtes sur la page 1sur 5

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

48

Android: How to Enable/Disable Wifi or Internet Connection Programmatically


android

Using the Connectivity Manager Class we can get access to either wifi or Internet Network:
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
// ...
}

where 0 and 1 respectively refers to mobile and wifi connection


If my Android device is connected to both, can we switch between any of the network or can we disable any of the network? Like using a function:
connec.getNetworkInfo(0).setState(NetworkInfo.State.DISCONNECTED);

share

improve this question


Javanator
8,938 6 32 53

Asked
Oct 14 '10 at 7:28

Swanand
8,763 6 31 49

Edited
Apr 1 '11 at 11:11

5 Answers

Order By

Votes

105

I know of enabling or disabling wifi:


WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);

where status may be true or false as per requirement.

share

improve this answer


viv
3,883 4 21 40

Answered
Oct 14 '10 at 7:45

Swanand
8,763 6 31 49

Edited
Apr 1 '11 at 11:12

Hi! Is there the same way to enable mobile networking? Yarovoy Jun 13 '12 at 21:20

34

Also at manifest: <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> Comic Sans MS Lover Jul 6 '12 at 14:07
Does this differ from wifiManager.reconnect()? What about wifiManager.reassociate()? Also in the approach above if you wanted to reset the WiFi could you call
wifiManager.setWifiEnabled(false) and wifiManager.setWifiEnabled(true) back to back? Or would you have to disable it first, then wait for some signal (broadcast receiver etc.)
before enabling it again? Marchy May 29 '13 at 23:50

If you want demo tutorial fot then its here : demoadda.com/demo/android/ Kishan Jul 18 '15 at 4:57

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> is also needed. zionpi May 13 at 9:16


add a comment

17

To Enable WiFi:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);

To Disable WiFi:

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


wifi.setWifiEnabled(false);

Note: To access with WiFi state, we have to add following permissions inside the AndroidManifest.xml file:
android.permission.ACCESS_WIFI_STATE
android.permission.UPDATE_DEVICE_STATS
android.permission.CHANGE_WIFI_STATE

share

improve this answer


Valeh Aayev
371 2 12

Answered
Sep 10 '12 at 5:07

only CHANGE_WIFI_STATE is necessary, at least in Android 4.2 voghDev Sep 2 '14 at 7:44
add a comment

complet Solution ..
try {
WifiManager wifi = (WifiManager)
context.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"SSIDName\"";
wc.preSharedKey = "\"password\"";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
boolean b=wifi.isWifiEnabled();
if(b){
wifi.setWifiEnabled(false);

............................................ http://amitkumar-android.blogspot.com/p/installation-steps.html

............................................ http://amitkumar-android.blogspot.com/p/installation-steps.html

share

improve this answer


Amit kumar
95 1 4

Answered
Jan 5 '11 at 19:57

pengwang
6,952 20 85 149

Edited
Sep 23 '11 at 1:37

The call to set the wifi manager with the wifi configuration is missing: wifi.addNetwork(wc); Error 454 Aug 1 '12 at 21:37

Why would you hard-code your SSID and password? Not a good idea. lifeson106 Mar 19 '14 at 15:51
add a comment

To enable disable Wifi use the WifiManager class to get system(android device) services for Wifi :
WifiManager wifi =(WifiManager)getSystemService(Context.WIFI_SERVICE);

Now the object wifi of the WifiManager class is used to get the wifi status:
if(wifi.isWifiEnabled())
//Perform Operation
else
//Other Operation

And most importantly do not forget to give the following permission in your Android Manifest File:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

To get detailed info and full sample code of the project for enable/disable Wifi on android visit my website link.

share

improve this answer


Hemant Mendi Ratta
114 4

Answered
Jun 14 '12 at 21:04

msysmilu
769 6 11

share

Edited
Jan 6 '15 at 13:54

add this permission in your manifest and than use the above code tochange wifi state: android.permission.CHANGE_WIFI_STATE

improve this answer


varun bhardwaj
1,111 10 18

Answered
May 18 '12 at 7:29

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Vous aimerez peut-être aussi