0

I want to turn off wifi for battery save on some screen where I don't need it.

I tried this:

WifiManager wifiManager = (WifiManager) SensorApplication.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) wifiManager.setWifiEnabled(false);

That works fine to enable wifi. To disable wifi it works but something like 2s later wifi automatically turns back on. I tried to look at the dev options if something can explain this but I disable all option.

If someone has an idea for explain why wifi automatically turn on when I turn off with this solution, or has another solution to disable wifi that would be great.

Thanks in advance.

theduck
  • 2,589
  • 13
  • 17
  • 23
Illuyankas
  • 150
  • 13

1 Answers1

0

As per the documentation https://developer.android.com/reference/kotlin/android/net/wifi/WifiManager#setWifiEnabled(kotlin.Boolean), setWifiEnabled() is deprecated . Are you targeting Q ?. Check the return value of this wifiManager.setWifiEnabled(false)

SwanCoder
  • 46
  • 3