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

152,514

Solution 1

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.

Edit:

You also need the following permissions in your manifest file:

 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 

Solution 2

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

Solution 3

A complete 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);
        Toast.makeText(context, "yes", Toast.LENGTH_SHORT).show();
    } else {
        wifi.setWifiEnabled(true);
        Toast.makeText(context, "no", Toast.LENGTH_SHORT).show();
    }
    //Log.d("WifiPreference", "enableNetwork returned " + b );

} catch (Exception e) {
    e.printStackTrace();
}

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

Solution 4

In Android Q (Android 10) you can't enable/disable wifi programmatically anymore. Use Settings Panel to toggle wifi connectivity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
    startActivityForResult(panelIntent, 0)
} else {
    // use previous solution, add appropriate permissions to AndroidManifest file (see answers above)
    (this.context?.getSystemService(Context.WIFI_SERVICE) as? WifiManager)?.apply { isWifiEnabled = true /*or false*/ }
}

Solution 5

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:
152,514

Related videos on Youtube

Rohit Sharma
Author by

Rohit Sharma

I am simple, happy and a programmer i am :)

Updated on December 22, 2021

Comments

  • Rohit Sharma
    Rohit Sharma over 2 years

    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 networks or can we disable any of the networks? Like using a function:

    connec.getNetworkInfo(0).setState(NetworkInfo.State.DISCONNECTED);
    
  • Comic Sans MS Lover
    Comic Sans MS Lover almost 12 years
    Also at manifest: <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  • Error 454
    Error 454 over 11 years
    The call to set the wifi manager with the wifi configuration is missing: wifi.addNetwork(wc);
  • Marchy
    Marchy almost 11 years
    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?
  • lifeson106
    lifeson106 about 10 years
    Why would you hard-code your SSID and password? Not a good idea.
  • Kishan Dhamat
    Kishan Dhamat almost 9 years
    If you want demo tutorial fot then its here : demoadda.com/demo/android/…
  • zionpi
    zionpi almost 8 years
    “<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />” is also needed.
  • Nithinjith
    Nithinjith about 7 years
    Is it possible to control WiFi in M device from Application?
  • Bertram Gilfoyle
    Bertram Gilfoyle about 5 years
    @lifeson106 I think it's only for the purpose of demonstration.
  • user1016765
    user1016765 almost 4 years
    Worth pointing to new/preferred method with example?

Related