Android: How to Enable/Disable Wifi or Internet Connection Programmatically
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"/>
Is it possible to programmatically enable wifi on Android 10 devices?
No, This is not possible to enable
or disable
Wi-Fi programmatically
from Android-10
API level 29
[Until google provides an alternative solution].
For applications targeting Build.VERSION_CODES.Q or above, this API will always return false and will have no effect.
If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.
There is an issue 128554616 which already has been created in google issuetracker
forum. You can see there for any updated info.
How to programmatically turn off WiFi on Android device?
You need the following permissions in your manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
Then you can use the following in your activity class:
WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);
Use the following to check if it's enabled or not
boolean wifiEnabled = wifiManager.isWifiEnabled()
You'll find a nice tutorial on the subject on this site.
Disabling wifi in android programmatically
You need to add this permission in your manifest file.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
Then add this code in your java file.
you need to add two buttons,one for enabling and one for disabling.
final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
enableWifi.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(!wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(true);
Toast.makeText(LogTest.this,"Wifi Enabled",Toast.LENGTH_SHORT).show();
}
}
});
disableWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
Toast.makeText(LogTest.this,"Wifi Disabled",Toast.LENGTH_SHORT).show();
}
}
});
Use the following to check if it's enabled or not
boolean wifiEnabled = wifiManager.isWifiEnabled();
android turning on wifi programmatically
You need to create wifiLock with WIFI_MODE_FULL_HIGH_PERF
mode, based on the docs it will only work with the following constraints:
- The lock is only active when the device is connected to an access point.
- The lock is only active when the screen is on.
- The lock is only active when the acquiring app is running in the foreground.
Android WifiManager not enabling/disabling WiFi state
This API is no longer supported when targeting Android 10 or higher.
Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.
Instead, you should use the Settings.Panel API to present a system UI allowing users to enable or disable Wi-Fi.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startActivity(Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY))
}
How to enable/disable WiFi from an application?
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" />
Related Topics
How to Pass an Array as Arguments to a Method With Variable Arguments in Java
"Eee Mmm Dd Hh:Mm:Ss Zzz Yyyy" Date Format to Java.Sql.Date
How to Get Android Wifi Scan Results into a List
Change File Owner Group Under Linux with Java.Nio.Files
Why Does Reading from Process' Inputstream Block Altough Data Is Available
Using Visualvm to Connect to a Remote Jstatd Instance Through a Firewall
How to Include Platform-Specific Native Libraries in the .Jar File Using Eclipse
Practical Limitations of Jvm Memory and CPU Usage
File Not Found Exception When Reading File from Linux Extreme Vps
How to Use Comparator to Define a Custom Sort Order
Looking for a CSS Parser in Java
Convert JSON Array to Normal Java List
How to Change Menu Item Text Dynamically in Android
How to Open a Command Terminal in Linux
Arval SQLexception: Fatal: Sorry, Too Many Clients Already in Postgres