35

I want to enable/disable wifi from my Android application. How can I do that?

0

6 Answers 6

52
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false); // true or false to activate/deactivate wifi

You also need to request the permission in your AndroidManifest.xml :

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Sign up to request clarification or add additional context in comments.

3 Comments

yes, don't forget to add use permission in your manifest android.permission.CHANGE_WIFI_STATE
@Codii, I know this is old, but I am trying to do this within a dialogfragment. However, it says "Cannot resolve method 'getSystemService(java.lang.String)' I am not sure on what I need to do. And I have those permissions
Worked for me thanks and you can check wifi if enable use false and if disable use true
18

To enable/disable WiFi in your application you need to use WiFiManager class. Create an Object of WiFiManager class to get the services of WiFi.

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

wifi.setWifiEnabled(false);//Turn off Wifi

wifi.setWifiEnabled(true);//Turn on Wifi

And you have to put the following permissions in AndroidManifest.xml

<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 the whole sample code of enable/disable Wifi in android with UI visit this website

2 Comments

This is a more complete answer. Includes all the required permissions.
when i do it in my project, but when these code is executed, the system will give a prompt says the app is trying to use wlan whether allows it. and it appears every time! can i enable wlan without the prompt window .
5

try this code

 Intent gpsOptionsIntent = new Intent(  android.provider.Settings.ACTION_WIFI_SETTINGS);  
            startActivityForResult(gpsOptionsIntent,0); 

1 Comment

thanks but i dont want to open settings. i want to disable or enable directly from my app.
5

To enable/disable wifi from an app in Android Q (Android 10) use Settings Panel:

val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
startActivityForResult(panelIntent, 0)

On previous versions of Android this should work (appropriate permissions should be added to AndroidManifest file, see answers above):

(context?.getSystemService(Context.WIFI_SERVICE) as? WifiManager)?.apply { isWifiEnabled = true /*or false*/ }

Resulting code might look something like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
    startActivityForResult(panelIntent, 0)
} else {
    (context?.getSystemService(Context.WIFI_SERVICE) as? WifiManager)?.apply { isWifiEnabled = true /*or false*/ }
}

Where context is a reference to android.content.Context object.

Comments

1

try this

public void disableWifi(Context context, Boolean bool) {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if(bool)
        wifi.setWifiEnabled(false);
    else
        wifi.setWifiEnabled(true);
}

1 Comment

Although this might answer the question, one should also explain how and why.
0
public class MainActivity extends AppCompatActivity {

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

        btn = (Switch) findViewById(R.id.switch1);
        btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    toggleWiFi(true);
                    Toast.makeText(getApplicationContext(), "Wi-Fi Enabled!", Toast.LENGTH_LONG).show();
                } else {
                    toggleWiFi(false);
                    Toast.makeText(getApplicationContext(), "Wi-Fi Disabled!", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    public void toggleWiFi(boolean status){
        WifiManager wifiManager = (WifiManager)this.getSystemService(WIFI_SERVICE);
        if (status && !wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        } else if (!status && wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }
}

Add User Permission in Manifest Files

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.