Turn Wifi On Off in Java Android Programmatically
If you want turn the Wi-Fi of an Android device on or off, you can try this code.
First, open the Android manifest file and type the wifi permission:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
These permissions are required for your app to read and change the device’s Wi-Fi states.
So, how to enable and disable android wifi with java code?
To turn on Wi-Fi, use this java code:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(true);
To turn Wi-Fi off, use this code:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);wifi.setWifiEnabled(false);
Let me know in the comments if this works for you 🙂