Some applications need to connect to the network, such as updating the backend service, refreshing the data, etc., the most common practice is to regularly network, directly using online resources, cache data or perform a download task to update the data.
But if the terminal is not connected to the network, or the speed is slow, there is no need to perform these tasks. You can use Connectivitymanager to check what the Internet is and what type of network is currently. Specific
The code is as follows:
Connectivitymanager cm = (connectivitymanager) Getapplicationcontext (). Getsystemservice ( Context.connectivity_service); = cm.getactivenetworkinfo (); Boolean // whether to connect to the network boolean Iswifi = activenetwork.gettype () = = Connectivitymanager.type_wifi;
Mobile networks consume more power than Wi-Fi, so in most cases apps should reduce the frequency of data refresh when they have a mobile network, while downloading large files while in the WiFi state. When the network is reconnected,
If you find that the current status is WiFi status, you can restart the previous stop download operation or refresh operation. Network connection state switching can be done by registering a broadcast receiver to the network status
Listening.
<android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/>
Monitoring of Android network connection status