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:
[Java]
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 static 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 networkInfo = 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 ();
// Determine 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 ";
Case 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 (networkInterfaceEnumeration. hasMoreElements ())
{
Final NetworkInterface networkInterface = networkInterfaceEnumeration. nextElement ();
Final Enumeration <InetAddress> inetAddressEnumeration = networkInterface. getInetAddresses ();
While (inetAddressEnumeration. hasMoreElements ())
{
Final InetAddress 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 = (ConnectivityManager) this. mContext. getSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo networkInfo = connectivityManager. getActiveNetworkInfo ();
If (networkInfo = null ){
Return NET_TYPE_NO_NETWORK;
} Else {
Return networkInfo. getTypeName ();
}
}
}
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 static 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 networkInfo = 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 ();
// Determine 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 ";
Case 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 (networkInterfaceEnumeration. hasMoreElements ())
{
Final NetworkInterface networkInterface = networkInterfaceEnumeration. nextElement ();
Final Enumeration <InetAddress> inetAddressEnumeration = networkInterface. getInetAddresses ();
While (inetAddressEnumeration. hasMoreElements ())
{
Final InetAddress 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 = (ConnectivityManager) this. mContext. getSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo networkInfo = connectivityManager. getActiveNetworkInfo ();
If (networkInfo = null ){
Return NET_TYPE_NO_NETWORK;
} Else {
Return networkInfo. getTypeName ();
}
}
} Do not forget to add the following permissions:
<! -- Get the network status permission -->
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/>