I want to enable/disable wifi from my Android application. How can I do that?
6 Answers
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" />

- 873
- 7
- 18
-
2yes, don't forget to add use permission in your manifest android.permission.CHANGE_WIFI_STATE – Codii Apr 15 '11 at 13:46
-
1@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 – James Robert Singleton Aug 11 '16 at 07:53
-
Worked for me thanks and you can check `wifi` if enable use `false` and if disable use `true` – SAYE Apr 28 '18 at 21:05
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

- 171
- 1
- 3
-
This is a more complete answer. Includes all the required permissions. – dubmojo Sep 18 '13 at 16:34
-
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 . – SalutonMondo Jun 03 '15 at 04:02
try this code
Intent gpsOptionsIntent = new Intent( android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivityForResult(gpsOptionsIntent,0);

- 2,250
- 3
- 25
- 35
-
1thanks but i dont want to open settings. i want to disable or enable directly from my app. – Mustafa İrer Apr 15 '11 at 10:59
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.

- 27,326
- 8
- 128
- 149
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);
}

- 12,778
- 14
- 93
- 110
-
1Although this might answer the question, one should also explain how and why. – BDL Aug 06 '15 at 10:33
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

- 15,689
- 25
- 79
- 125