Home > Java > javaTutorial > How to Enable or Disable Wi-Fi Programmatically in Android?

How to Enable or Disable Wi-Fi Programmatically in Android?

Susan Sarandon
Release: 2024-11-09 05:24:02
Original
798 people have browsed it

How to Enable or Disable Wi-Fi Programmatically in Android?

How to Control Network Connectivity in Android

The Connectivity Manager class provides access to network information and allows for management of network connectivity. By utilizing this class, you can determine whether your Android device is connected to a network, either via mobile data or Wi-Fi.

Enabling or Disabling Wi-Fi

To enable or disable Wi-Fi programmatically, use the following code:

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);
Copy after login

where status can be true to enable Wi-Fi or false to disable it.

Permissions Required

For this functionality to work, you will need to declare the following permissions in your Android manifest file:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Copy after login

Notes

It's important to note that this approach only controls Wi-Fi connectivity. To manage mobile data connectivity, you will need to explore other methods such as using the ConnectivityManager class or the TelephonyManager class.

The above is the detailed content of How to Enable or Disable Wi-Fi Programmatically in Android?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template