安卓 获取手机IP地址的实现代码
1.获取手机IP地址的代码:
publicstaticStringgetLocalIpAddress(){ try{ for(Enumeration<NetworkInterface>en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){ NetworkInterfaceintf=en.nextElement(); for(Enumeration<InetAddress>enumIpAddr=intf .getInetAddresses();enumIpAddr.hasMoreElements();){ InetAddressinetAddress=enumIpAddr.nextElement(); if(!inetAddress.isLoopbackAddress()){ returninetAddress.getHostAddress().toString(); } } } }catch(SocketExceptione){ //TODO:handleexception Utils.log("WifiPreferenceIpAddress---error-"+e.toString()); } returnnull; }
但是在4.0下会出现类似fe80::b607:f9ff:fee5:487e的IP地址,这个是IPV6的地址,我们需要获得是的IPV4的地址,所以要在上诉代码中加一个判断
InetAddressUtils.isIPv4Address(inetAddress.getHostAddress());
2.完整代码如下:
publicstaticStringgetLocalIpAddress(){ try{ for(Enumeration<NetworkInterface>en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){ NetworkInterfaceintf=en.nextElement(); for(Enumeration<InetAddress>enumIpAddr=intf .getInetAddresses();enumIpAddr.hasMoreElements();){ InetAddressinetAddress=enumIpAddr.nextElement(); if(!inetAddress.isLoopbackAddress()&&InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())){ returninetAddress.getHostAddress().toString(); } } } }catch(SocketExceptione){ //TODO:handleexception Utils.log("WifiPreferenceIpAddress---error-"+e.toString()); } returnnull; }
获取android手机当前ip地址
publicclassNetWorkUtils{ /** *检查网络是否可用 * *@paramparamContext *@return */ publicstaticbooleancheckEnable(ContextparamContext){ booleani=false; NetworkInfolocalNetworkInfo=((ConnectivityManager)paramContext .getSystemService("connectivity")).getActiveNetworkInfo(); if((localNetworkInfo!=null)&&(localNetworkInfo.isAvailable())) returntrue; returnfalse; } /** *将ip的整数形式转换成ip形式 * *@paramipInt *@return */ publicstaticStringint2ip(intipInt){ StringBuildersb=newStringBuilder(); sb.append(ipInt&0xFF).append("."); sb.append((ipInt>>8)&0xFF).append("."); sb.append((ipInt>>16)&0xFF).append("."); sb.append((ipInt>>24)&0xFF); returnsb.toString(); } /** *获取当前ip地址 * *@paramcontext *@return */ publicstaticStringgetLocalIpAddress(Contextcontext){ try{ //for(Enumeration<NetworkInterface>en=NetworkInterface //.getNetworkInterfaces();en.hasMoreElements();){ //NetworkInterfaceintf=en.nextElement(); //for(Enumeration<InetAddress>enumIpAddr=intf //.getInetAddresses();enumIpAddr.hasMoreElements();){ //InetAddressinetAddress=enumIpAddr.nextElement(); //if(!inetAddress.isLoopbackAddress()){ //returninetAddress.getHostAddress().toString(); //} //} //} WifiManagerwifiManager=(WifiManager)context .getSystemService(Context.WIFI_SERVICE); WifiInfowifiInfo=wifiManager.getConnectionInfo(); inti=wifiInfo.getIpAddress(); returnint2ip(i); }catch(Exceptionex){ return"获取IP出错鸟!!!!请保证是WIFI,或者请重新打开网络!\n"+ex.getMessage(); } //returnnull; } }
Android中获取本机ip地址和MAC地址
通过InetAddress.getLocalHost()得到始终是“127.0.0.1”,要想得到真正的网络ip地址要通过下面的方法:
首先新建一个工程,修改AndroidManifest.xml文件增加用户权限,如下:
<uses-permissionandroid:name="android.permission.INTERNET"/>//必写 <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission> <uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>//必写 <uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
主要函数代码如下:
//得到本机ip地址 publicStringgetLocalHostIp() { Stringipaddress=""; try { Enumeration<NetworkInterface>en=NetworkInterface .getNetworkInterfaces(); //遍历所用的网络接口 while(en.hasMoreElements()) { NetworkInterfacenif=en.nextElement();//得到每一个网络接口绑定的所有ip Enumeration<InetAddress>inet=nif.getInetAddresses(); //遍历每一个接口绑定的所有ip while(inet.hasMoreElements()) { InetAddressip=inet.nextElement(); if(!ip.isLoopbackAddress() &&InetAddressUtils.isIPv4Address(ip .getHostAddress())) { returnipaddress="本机的ip是"+":"+ip.getHostAddress(); } } } } catch(SocketExceptione) { Log.e("feige","获取本地ip地址失败"); e.printStackTrace(); } returnipaddress; } //得到本机Mac地址 publicStringgetLocalMac() { Stringmac=""; //获取wifi管理器 WifiManagerwifiMng=(WifiManager)getSystemService(Context.WIFI_SERVICE); WifiInfowifiInfor=wifiMng.getConnectionInfo(); mac="本机的mac地址是:"+wifiInfor.getMacAddress(); returnmac; }
Android获取wifi的IP地址
WifiManagerwifimanage=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);//获取WifiManager //检查wifi是否开启 if(!wifimanage.isWifiEnabled()){ wifimanage.setWifiEnabled(true); } WifiInfowifiinfo=wifimanage.getConnectionInfo(); Stringip=intToIp(wifiinfo.getIpAddress()); //将获取的int转为真正的ip地址,参考的网上的,修改了下 privateStringintToIp(inti){ return(i&0xFF)+"."+((i>>8)&0xFF)?+"."+((i>>16)&0xFF)+"."+((i>>24)&0xFF); }
OK,这样就好了吗?呵呵,别忘记加上权限
<uses-permission=""android:name="android.permission.ACCESS_WIFI_STATE"></uses> <uses-permission=""android:name="adnroid.permission.CHANGE_WIFI_STATE"></uses>
总结:大家可以对比一下,Android获取手机IP 地址的方法,以免在编程的过程中造成不必要的问题.