public static String getLocalIpAddress(){
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()) {
return inetAddress.getHostAddress().toString();
}
}
}
}catch (SocketException e) {
// TODO: handle exception
Utils.log("WifiPreference IpAddress---error-" + e.toString());
}
return null;
}
InetAddressUtils.isIPv4Address(inetAddress.getHostAddress());
public static String getLocalIpAddress(){
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() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
return inetAddress.getHostAddress().toString();
}
}
}
}catch (SocketException e) {
// TODO: handle exception
Utils.log("WifiPreference IpAddress---error-" + e.toString());
}
return null;
}
public class NetWorkUtils {
/**
* 检查网络是否可用
*
* @param paramContext
* @return
*/
public static boolean checkEnable(Context paramContext) {
boolean i = false;
NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext
.getSystemService("connectivity")).getActiveNetworkInfo();
if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable()))
return true;
return false;
}
/**
* 将ip的整数形式转换成ip形式
*
* @param ipInt
* @return
*/
public static String int2ip(int ipInt) {
StringBuilder sb = new StringBuilder();
sb.append(ipInt & 0xFF).append(".");
sb.append((ipInt >> 8) & 0xFF).append(".");
sb.append((ipInt >> 16) & 0xFF).append(".");
sb.append((ipInt >> 24) & 0xFF);
return sb.toString();
}
/**
* 获取当前ip地址
*
* @param context
* @return
*/
public static String getLocalIpAddress(Context context) {
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()) {
// return inetAddress.getHostAddress().toString();
// }
// }
// }
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int i = wifiInfo.getIpAddress();
return int2ip(i);
} catch (Exception ex) {
return " 获取IP出错鸟!!!!请保证是WIFI,或者请重新打开网络!\n" + ex.getMessage();
}
// return null;
}
}
// 得到本机ip地址
public String getLocalHostIp()
{
String ipaddress = "";
try
{
Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces();
// 遍历所用的网络接口
while (en.hasMoreElements())
{
NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
Enumeration<InetAddress> inet = nif.getInetAddresses();
// 遍历每一个接口绑定的所有ip
while (inet.hasMoreElements())
{
InetAddress ip = inet.nextElement();
if (!ip.isLoopbackAddress()
&& InetAddressUtils.isIPv4Address(ip
.getHostAddress()))
{
return ipaddress = "本机的ip是" + ":" + ip.getHostAddress();
}
}
}
}
catch (SocketException e)
{
Log.e("feige", "获取本地ip地址失败");
e.printStackTrace();
}
return ipaddress;
}
// 得到本机Mac地址
public String getLocalMac()
{
String mac = "";
// 获取wifi管理器
WifiManager wifiMng = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfor = wifiMng.getConnectionInfo();
mac = "本机的mac地址是:" + wifiInfor.getMacAddress();
return mac;
}
WifiManager wifimanage=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);//获取WifiManager
//检查wifi是否开启
if(!wifimanage.isWifiEnabled()) {
wifimanage.setWifiEnabled(true);
}
WifiInfo wifiinfo= wifimanage.getConnectionInfo();
String ip=intToIp(wifiinfo.getIpAddress());
//将获取的int转为真正的ip地址,参考的网上的,修改了下
private String intToIp(int i) {
return (i & 0xFF)+ "." + ((i >> 8 ) & 0xFF)? + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF );
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有