Turn WiFi On or Off Programmatic in Java Android Studio

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 🙂

Related Posts

Fix Plugin [id: ‘com.android.application’, version: ‘7.1.3’, apply: false] was not found in any of the following sources

Here is how to fix Plugin [id: ‘com.android.application’, version: ‘7.1.3’, apply: false] was not found in any of the following sources This error is caused by mismatching…

Get Age from Date of Birth in Java Android

Here is how to get a user’s age from date of birth If you need get a user’s age from a given date of birth string, this…

How to Check if String Array Contains a Specific String in Android

Here is how to get a specific value or String from Array in Android To check if a string array contains a specific string, you can use…

Make Android WebView Support Razorpay Payment

How to Support Razorpay Payment and UPI in Android WebView? Android webview is inexpensive and easy way to make apps for websites. But to support some specific…

Make Android Webview Support PayTM Google Pay PhonePe UPI

GooglePay, Phonpe and PayTM support in Android WebView If you have built an android app for your new ecommerce website or any other website that needs accept…

Comment