安卓工具类之获取网络状态工具类,android设计模式原则

try {

if (_CM.getActiveNetworkInfo() != null) {

if (_CM.getActiveNetworkInfo().isAvailable()) {

return true;

}

}

} catch (Exception e) {

return false;

}

}

return false;

}

return false;

}

/**

  • 获取Wifi是否可用

  • @param pContext Context

  • @return true:isAvailable; false:otherwise;

*/

public static boolean isWiFiActive(Context pContext) {

if (pContext == null)

return false;

boolean _IsAvailable = false;

WifiManager _WifiManager;

WifiInfo _WifiInfo;

try{

_WifiManager = (WifiManager)pContext.getSystemService(Context.WIFI_SERVICE);

_WifiInfo = _WifiManager.getConnectionInfo();

int _IpAddress = _WifiInfo == null ? 0 : _WifiInfo.getIpAddress();

if (_WifiManager.isWifiEnabled() && _IpAddress != 0) {

_IsAvailable = true;

}

}catch(Exception e){

_IsAvailable = false;

}

return _IsAvailable;

}

/**

  • 获取当前的网络名称(WIFI or 2G or 3G or 4G)

  • @param pContext Context

  • @return WIFI or 2G or 3G or 4G

*/

public static String getNetworkName(Context pContext) {

try {

ConnectivityManager _CM = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo _NetworkInfo = _CM.getActiveNetworkInfo();

if (_NetworkInfo == null || !_NetworkInfo.isConnected()) {

return NET_NO; //not connected

}

if (_NetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {

return NET_WIFI;

}

if (_NetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {

int networkType = _NetworkInfo.getSubtype();

switch (networkType) {

case TelephonyManager.NETWORK_TYPE_GPRS:

case TelephonyManager.NETWORK_TYPE_EDGE:

case TelephonyManager.NETWORK_TYPE_CDMA:

case TelephonyManager.NETWORK_TYPE_1xRTT:

case TelephonyManager.NETWORK_TYPE_IDEN:

return NET_2G;

case TelephonyManager.NETWORK_TYPE_UMTS:

case TelephonyManager.NETWORK_TYPE_EVDO_0:

case TelephonyManager.NETWORK_TYPE_EVDO_A:

case TelephonyManager.NETWORK_TYPE_HSDPA:

case TelephonyManager.NETWORK_TYPE_HSUPA:

case TelephonyManager.NETWORK_TYPE_HSPA:

case TelephonyManager.NETWORK_TYPE_EVDO_B:

case TelephonyManager.NETWORK_TYPE_EHRPD:

case TelephonyManager.NETWORK_TYPE_HSPAP:

return NET_3G;

case TelephonyManager.NETWORK_TYPE_LTE:

return NET_4G;

}

}

} catch (Exception e) {

return NET_NO;

}

return NET_NO;

}

/**

  • 获取 Wifi 名称

  • @param pContext

  • @return

*/

public static String getWifiName(Context pContext) {

String _WifiName = “”;

WifiManager _WifiManager = (WifiManager) pContext.getSystemService(Context.WIFI_SERVICE);

_WifiName = _WifiManager.getConnectionInfo().getSSID();

return _WifiName;

}

/**

  • 获取 Wifi状态

  • @param pContext Context

  • @return

*/

public static int getWifiStatus(Context pContext) {

return ((WifiManager) pContext.getSystemService(Context.WIFI_SERVICE)).getWifiState();

}

/**

  • 获取 Wifi IP

  • @param pContext

  • @return

*/

public static String getHostIp(Context pContext) {

String _HostIp = “”;

try {

WifiManager _WifiManager = (WifiManager) pContext.getSystemService(Context.WIFI_SERVICE);

int i;

if (_WifiManager.isWifiEnabled()) {

i = _WifiManager.getConnectionInfo().getIpAddress();

if (i != 0)

_HostIp = (i & 0xFF) + “.” + (0xFF & i >> 8) + “.”

  • (0xFF & i >> 16) + “.” + (0xFF & i >> 24);

} else {

i = getWifiStatus(pContext);

if ((i == 2) || (i == 3)) {

Enumeration<?> _Enumeration = NetworkInterface.getNetworkInterfaces();

while (_Enumeration.hasMoreElements()) {

Enumeration<?> _EnumerationTemp = ((NetworkInterfac安卓工具类之获取网络状态工具类,android设计模式原则
e) _Enumeration

.nextElement()).getInetAddresses();

if (!_EnumerationTemp.hasMoreElements())

;

while (true) {

if (_HostIp.length() <= 0)

break;

InetAddress _LocalInetAddress = (InetAddress) _EnumerationTemp

.nextElement();

if ((_LocalInetAddress.isLoopbackAddress())

|| (!(_LocalInetAddress instanceof Inet4Address)))

break;

_HostIp = _LocalInetAddress.getHostAddress();

(_HostIp.length() <= 0)

break;

InetAddress _LocalInetAddress = (InetAddress) _EnumerationTemp

.nextElement();

if ((_LocalInetAddress.isLoopbackAddress())

|| (!(_LocalInetAddress instanceof Inet4Address)))

break;

_HostIp = _LocalInetAddress.getHostAddress();

上一篇:vue中dom模板为什么要使用kebab-case命名法(短横线命名)


下一篇:算法练习:有效的括号