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

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:

  1. The lock is only active when the device is connected to an access point.
  2. The lock is only active when the screen is on.
  3. 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



Leave a reply



Submit