Home » Android » How to Enable/Disable WiFi problematically in Android ?

How to Enable/Disable WiFi problematically in Android ?

Using “setWifiEnabled” API in Android, you can enable or disable Wi-Fi programmatically. Below an example of how to do this in an Android app:

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class WifiControlActivity extends AppCompatActivity {

    private Button enableWifiButton;
    private Button disableWifiButton;
    private WifiManager wifiManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_control);

        // Initialize the WifiManager
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        enableWifiButton = findViewById(R.id.enableWifiButton);
        disableWifiButton = findViewById(R.id.disableWifiButton);

        enableWifiButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enableWifi();
            }
        });

        disableWifiButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                disableWifi();
            }
        });
    }

    private void enableWifi() {
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
    }

    private void disableWifi() {
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}

in this example:

  1. We have an WifiControlActivity that contains two buttons (enableWifiButton and disableWifiButton) and a WifiManager object.
  2. In the onCreate method, we initialize the WifiManager using getSystemService(Context.WIFI_SERVICE).
  3. We set OnClickListener for both buttons to enable and disable Wi-Fi when they are clicked.
  4. The enableWifi method checks if Wi-Fi is not already enabled and then uses setWifiEnabled(true) to enable it.
  5. The disableWifi method checks if Wi-Fi is enabled and then uses setWifiEnabled(false) to disable it.

Make sure you have the appropriate permissions declared in your AndroidManifest.xml file to control Wi-Fi:

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

Also, you’ll need to create the corresponding XML layout (activity_wifi_control.xml) to define the UI with the two buttons.

Limitation :

The API “setWiFiEnabled” has been deprecated for using for normal application after API level 29 or Android 10. Hence we can’t turn ON / OFF the wifi using the example mentioned above.

Solution :

The only solution to use the “setWiFiEnabled” API to turn ON / OFF the WiFi after Android 10 is to make sure our application as system UID and the application has been signed with the platform key.


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment