Determines whether there is a network connection public static Boolean isnetworkconnected (context context) {if (context! = null) {Co Nnectivitymanager Mconnectivitymanager = (connectivitymanager) context. Getsystemservice (Context.connec Tivity_service); Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (mnetworkinfo! = null) {return mnetworkinfo.isavailable (); }} return false; }//Determine if the WiFi network is available public static Boolean iswificonnected (context context) {if (context! = null) { Connectivitymanager Mconnectivitymanager = (connectivitymanager) context. Getsystemservice (context.c Onnectivity_service); Networkinfo mwifinetworkinfo = Mconnectivitymanager. Getnetworkinfo (Connectivitymanager.type_wifi); if (mwifinetworkinfo! = null) {return mwifinetworkinfo.isavailable (); } } return false; }//Determine if the mobile network is available public static Boolean ismobileconnected (context context) {if (context! = null) { Connectivitymanager Mconnectivitymanager = (connectivitymanager) context. Getsystemservice (Conte Xt. Connectivity_service); Networkinfo mmobilenetworkinfo = Mconnectivitymanager. Getnetworkinfo (Connectivitymanager.type_mobile); if (mmobilenetworkinfo! = null) {return mmobilenetworkinfo.isavailable (); }} return false; }//Gets the type information of the current network connection public static int Getconnectedtype (context context) {if (context! = null) { Connectivitymanager Mconnectivitymanager = (connectivitymanager) context. Getsystemservice (Context.con Nectivity_service); Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (mnetworkinfo! = null && mnetworkinfo.isavailable ()) { return Mnetworkinfo.gettype (); }} return-1; }
Some common functions of Android to Judge network status