In our work, we often need to use the network and check the network status is essential. This is a tool class for checking the current network status. It can be directly pasted in the past:
Package COM. todoo. android. app. utils; import java.net. inetaddress; import java.net. networkinterface; import java.net. socketexception; import Java. util. enumeration; import android. content. context; import android.net. connectivitymanager; import android.net. networkinfo; public class networkutils {public static final string net_type_wifi = "WiFi"; public static final string net_type_mobile = "mobile"; Public stati C Final string net_type_no_network = "no_network"; private context mcontext = NULL; Public networkutils (context pcontext) {This. mcontext = pcontext;} public static final string ip_default = "0.0.0.0"; public static Boolean isconnectinternet (final context pcontext) {final connectivitymanager conmanager = (connectivitymanager) pcontext. getsystemservice (context. connectivity_service); Final networkinfo netw Orkinfo = conmanager. getactivenetworkinfo (); If (networkinfo! = NULL) {return networkinfo. isavailable () ;}return false;} public static Boolean isconnectwifi (final context pcontext) {connectivitymanager mconnectivity = (connectivitymanager) pcontext. getsystemservice (context. connectivity_service); networkinfo info = mconnectivity. getactivenetworkinfo (); // determines the network connection type. Only data in 3G or WiFi is updated. Int nettype =-1; if (info! = NULL) {nettype = info. getType ();} If (nettype = connectivitymanager. type_wifi) {return info. isconnected () ;}else {return false ;}} public static string getnettypename (final int pnettype) {Switch (pnettype) {Case 0: Return "unknown"; Case 1: return "GPRS"; Case 2: Return "edge"; Case 3: Return "UMTS"; Case 4: Return "CDMA: either is95a or is95b"; Case 5: return "evdo Revision 0"; Case 6: Return "evdo Revision A"; CA Se 7: Return "1 xrtt"; case 8: Return "HSDPA"; Case 9: Return "hsupa"; case 10: Return "HSPA"; Case 11: return "iden"; Case 12: Return "evdo Revision B"; Case 13: Return "LTE"; Case 14: Return "ehrpd"; Case 15: Return "HSPA + "; default: Return "unknown" ;}} public static string getipaddress () {try {final enumeration <networkinterface> networkinterfaceenumeration = networkinterface. getnetworkinterfaces (); While (networkinter Faceenumeration. hasmoreelements () {final networkinterface = networkinterfaceenumeration. nextelement (); Final enumeration <inetaddress> inetaddressenumeration = networkinterface. getinetaddresses (); While (inetaddressenumeration. hasmoreelements () {final inetaddress = inetaddressenumeration. nextelement (); If (! Inetaddress. isloopbackaddress () {return inetaddress. gethostaddress () ;}} return networkutils. ip_default;} catch (final socketexception e) {return networkutils. ip_default ;}} Public String getconntypename () {connectivitymanager = (connectivitymanager) This. mcontext. getsystemservice (context. connectivity_service); networkinfo = connectivitymanager. getactivenetworkinfo (); If (networkinfo = NULL) {return net_type_no_network;} else {return networkinfo. gettypename ();}}}
Do not forget to add the following permissions: <! --
Get network status permissions -->
<Uses-Permission Android: Name = "android. Permission. access_network_state"/>
<Uses-Permission Android: Name = "android. Permission. access_wifi_state"/>