Home >Java >javaTutorial >How to Programmatically Control WiFi and Internet Connections in Android?
Programmatic Control of WiFi and Internet Connections in Android
In Android, the Connectivity Manager provides access to network information and connectivity. However, developers may encounter scenarios where they need to enable, disable, or switch between WiFi and mobile internet connections programmatically.
Using the provided code snippet, it is possible to determine if the device is connected to WiFi or mobile data. However, there is no direct method to change the network status using the Connectivity Manager.
To enable or disable WiFi, you can utilize the WifiManager class:
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(status);
Where status should be set to true to enable WiFi and false to disable it.
Manifest Permissions:
To utilize the WifiManager functionality, ensure the following permissions are added to your manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
By leveraging these permissions and the WifiManager class, developers can programmatically control WiFi connections in Android, allowing for finer-grained network management in their applications.
The above is the detailed content of How to Programmatically Control WiFi and Internet Connections in Android?. For more information, please follow other related articles on the PHP Chinese website!