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.
To enable or disable Wi-Fi programmatically, use the following code:
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(status);
where status can be true to enable Wi-Fi or false to disable it.
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"/>
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!