Search code examples
androidgeolocationandroid-wifi

Turn off/on wifi by wifi automatically by geolocation in Android


In my application I want to implement this feature: Turn Off or Turn On the wifi using the geolocation.

I mean, for example, when I come back home my wifi turn on and when go away it turns off. It would be best if the user gives the location as input. Does anyone have any sample code about it? For now to me even if the location is written by me directly in Java it's good too.


Solution

  • I do not know about geo location, but turning Wifi on/off can be done using the code below

    WifiManager wifiManager = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
    

    To Enable

    wifiManager.setWifiEnabled(true);
    

    To Disable

    wifiManager.setWifiEnabled(false);