When the Android app needs to connect to the network, it does not connect to the network every time, so the program needs to detect the current device's network status in order to alert the user in a timely manner.
To determine the status of the network, you need to have the appropriate permission code as follows (androidmanifest.xml):
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android: Name= "Android.permission.INTERNET"/>
code to detect the current network status:
public void Checknetworkstate (context context) {if (context! = null) {Connectivitymanager Mconnectivitymanager = ( Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo networkinfo = Mconnectivitymanager.getactivenetworkinfo ();//1. Determine if there is an Internet connection Boolean networkavailable = Networkinfo.isavailable ();//2. Gets the type information for the current network connection int networktype = Networkinfo.gettype (); if (connectivitymanager.type_ WiFi = = Networktype) {//current WiFi network}else if (connectivitymanager.type_mobile = = Networktype) {//Current MOBILE Network}}}
Android Detects network connection status