其他分享
首页 > 其他分享> > 安卓工具类之获取网络状态工具类,android设计模式原则

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

作者:互联网

try {

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

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

return true;

}

}

} catch (Exception e) {

return false;

}

}

return false;

}

return false;

}

/**

*/

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;

}

/**

*/

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;

}

/**

*/

public static String getWifiName(Context pContext) {

String _WifiName = “”;

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

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

return _WifiName;

}

/**

*/

public static int getWifiStatus(Context pContext) {

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

}

/**

*/

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) + “.”

} else {

i = getWifiStatus(pContext);

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

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

while (_Enumeration.hasMoreElements()) {

Enumeration<?> _EnumerationTemp = ((NetworkInterfac
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();

标签:case,return,pContext,安卓,TelephonyManager,WifiManager,工具,设计模式,TYPE
来源: https://blog.csdn.net/m0_66265052/article/details/122642191