android设备获取当前所用网络类型和获取手机ip地址
2015-04-24 09:32
871 查看
/**
* 得到当前的手机网络类型
*
* @param context
* @return
*/
public static String getCurrentNetType(Context context) {
String type = "";
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null) {
type = "null";
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
type = "wifi";
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
int subType = info.getSubtype();
if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_GPRS
|| subType == TelephonyManager.NETWORK_TYPE_EDGE) {
type = "2g";
} else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_HSDPA
|| subType == TelephonyManager.NETWORK_TYPE_EVDO_A || subType == TelephonyManager.NETWORK_TYPE_EVDO_0
|| subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
type = "3g";
} else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡,是3.9G的全球标准
type = "4g";
}
}
LogProxy.d("QZY", "type=" + type);
return type;
}
/**
* 获取手机ip地址
*
* @return
*/
public static String getPhoneIp() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
// if (!inetAddress.isLoopbackAddress() && inetAddress
// instanceof Inet6Address) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception e) {
}
return "";
}
转载自:http://www.open-open.com/lib/view/open1409190432572.html
* 得到当前的手机网络类型
*
* @param context
* @return
*/
public static String getCurrentNetType(Context context) {
String type = "";
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null) {
type = "null";
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
type = "wifi";
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
int subType = info.getSubtype();
if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_GPRS
|| subType == TelephonyManager.NETWORK_TYPE_EDGE) {
type = "2g";
} else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_HSDPA
|| subType == TelephonyManager.NETWORK_TYPE_EVDO_A || subType == TelephonyManager.NETWORK_TYPE_EVDO_0
|| subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
type = "3g";
} else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡,是3.9G的全球标准
type = "4g";
}
}
LogProxy.d("QZY", "type=" + type);
return type;
}
/**
* 获取手机ip地址
*
* @return
*/
public static String getPhoneIp() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
// if (!inetAddress.isLoopbackAddress() && inetAddress
// instanceof Inet6Address) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception e) {
}
return "";
}
转载自:http://www.open-open.com/lib/view/open1409190432572.html
相关文章推荐
- Android程序:获取当前手机的网络类型
- 获取Android当前网络类型(2g、3g、4g、wifi)以及手机型号、版本号
- Android获取当前手机网络类型(2g、3g、4g、wifi)以及手机型号、版本号代码
- Android获取如何获取当前手机IP地址
- android 获取手机上面的IMSI序列号,IMEI,MAC地址,IP地址,当前时间,当前经纬度,获取网络类型
- 获取Android当前网络类型(2g、3g、4g、wifi)以及手机型号、版本号
- 获取android手机当前网络链接IP的方法
- 如何获取Android手机连接网络的当前局域网IP
- 如何获取Android手机连接当前网络的外网IP
- android设备无线、有线网络下获取MAC地址
- Android 手机获取当前IP
- Android获取手机IP地址
- android 判断当前环境网络是否可用及获取当前网络类型
- Android开发中检测网络和获取手机设备和防暴力点击工具类
- ios获取手机状态 idfa idfv 网络类型 分辨率 获取运营商 ip
- android获取当前手机设备信息工具类
- 朝花夕拾-android 获取当前手机的内存卡状态和网络连接状态
- Android 判断网络是否可用、网络类型WIFI/2G/3G/4G及获取IP
- Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
- 记录-Android根据手机gps或者网络获取当前位置