The Connectivity Manager can also be used to control network hardware and configure failover preferences.
Android will attempt to connect to the preferred network whenever an authorized application requests an Internet connection. You can find the current, and set the preferred, network using the getNetworkPreference and setNetworkPreference methods, respectively, as shown in the following code snippet:
int networkPreference = connectivity.getNetworkPreference(); connectivity.setNetworkPreference(NetworkPreference.PREFER_WIFI);
If the preferred connection is unavailable, or connectivity on this network is lost, Android will automatically attempt to connect to the secondary network.
You can control the availability of the network types using the setRadio method. This method lets you set the state of the radio associated with a particular network (Wi-Fi, mobile, etc.). For example, in the following code snippet the Wi-Fi radio is turned off and the mobile radio is turned on:
connectivity.setRadio(NetworkType.WIFI, false); connectivity.setRadio(NetworkType.MOBILE, true);
Was this article helpful?
Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.