简体   繁体   English

如何在应用程序关闭时自动关闭 WiFi

[英]How to turn OFF WiFi automatically when the app closes

I have already turned ON WiFi using this:我已经使用这个打开了 WiFi:

 WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if (wifiManager != null)
        wifiManager.setWifiEnabled(true);

Now I want to turn it off when I close my app I have tried by using onDestory() method it does not work guide me.现在我想在关闭我的应用程序时将其关闭我尝试使用 onDestory() 方法它不起作用指导我。

try to setWifiEnabled(false) before calling super.onDestroy()在调用super.onDestroy() ) 之前尝试设置setWifiEnabled(false) )

@Override
public void onDestroy() {
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    if(wifiManager!=null) wifiManager.setWifiEnabled(false);
    super.onDestroy();
}

note that this method is deprecated in API29 and above, will return false and won't change WiFi state when your app will target API29 or above (which is, or will be soon, mandatory for publishing in Google Play)请注意,此方法在 API29 及更高版本中已弃用,当您的应用程序将以 API29 或更高版本为目标时,将返回false并且不会更改 WiFi state(这是或即将在 Google Play 中发布的强制性要求)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM