简体   繁体   中英

How to turn OFF WiFi automatically when the app closes

I have already turned ON WiFi using this:

 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.

try to setWifiEnabled(false) before calling super.onDestroy()

@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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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