Example Analysis of android Network tool Class
This article mainly introduces the example analysis of android network tools, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
In the actual development, often some tool classes are very helpful to us. Thus, on the basis of my predecessors, I have sorted out a network tool class, and hereby present:
/ * * @ Class name: NetUtil * @ Class description: network judgment processing Class * @ creation time: February 12, 2015-9:34:32 * @ modifier: * @ revision Note: * @ version: * / public class NetUtil {/ * Network status * / public static boolean isNet = true Public static enum netType {wifi, CMNET, CMWAP, noneNet} / * @ method description: determine whether the WIFI network is available by * @ method name: isWifiConnected * @ param context * @ return * @ return value: boolean * / public static boolean isWifiConnected (Context context) {if (context! = null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService (Context.CONNECTIVITY_SERVICE) NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo (ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo! = null) {return mWiFiNetworkInfo.isAvailable ();}} return false } / * * @ method description: determine whether the MOBILE network is available by * @ method name: isMobileConnected * @ param context * @ return * @ return value: boolean * / public static boolean isMobileConnected (Context context) {if (context! = null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService (Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo (ConnectivityManager.TYPE_MOBILE) If (mMobileNetworkInfo! = null) {return mMobileNetworkInfo.isAvailable ();}} return false } / * * @ method description: get the type information of the current network connection * @ method name: getConnectedType * @ param context * @ return * @ return value: int * / public static int getConnectedType (Context context) {if (context! = null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService (Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo () If (mNetworkInfo! = null & & mNetworkInfo.isAvailable ()) {return mNetworkInfo.getType ();}} return-1 } / * * @ method description: get the current network status-1: no network 1:WIFI network 2:wap network 3:net network * @ method name: getAPNType * @ param context * @ return * @ return value: netType * / public static netType getAPNType (Context context) {ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService (Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo () If (networkInfo = = null) {return netType.noneNet;} int nType = networkInfo.getType (); if (nType = = ConnectivityManager.TYPE_MOBILE) {if (networkInfo.getExtraInfo (). ToLowerCase (). Equals ("cmnet")) {return netType.CMNET;} else {return netType.CMWAP }} else if (nType = = ConnectivityManager.TYPE_WIFI) {return netType.wifi;} return netType.noneNet } / * * @ method description: determine whether there is a network connection * @ method name: isNetworkConnected * @ param context * @ return * @ return value: boolean * / public static boolean isNetworkConnected (Context context) {if (context! = null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService (Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo () If (mNetworkInfo! = null) {return mNetworkInfo.isAvailable ();}} return false;} / * @ method description: whether the network is available * @ method name: isNetworkAvailable * @ param context * @ return * @ return value: boolean * / public static boolean isNetworkAvailable (Context context) {ConnectivityManager mgr = (ConnectivityManager) context .getSystemService (Context.CONNECTIVITY_SERVICE) NetworkInfo [] info = mgr.getAllNetworkInfo (); if (info! = null) {for (int I = 0; I < info.length; iTunes +) {if (info[ I] .getState () = = NetworkInfo.State.CONNECTED) {return true;}} return false } / * * @ method description: determine whether it is a mobile network * @ method name: is3GNet * @ param context * @ return * @ return value: boolean * / public static boolean is3GNet (Context context) {ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService (Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo () If (activeNetInfo! = null & & activeNetInfo.getType () = ConnectivityManager.TYPE_MOBILE) {return true;} return false;}} Thank you for reading this article carefully. I hope the article "sample Analysis of android Network tools" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!