1.獲取手機IP地址的代碼:
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; }但是在4.0 下 會出現(xiàn)類似fe80::b607:f9ff:fee5:487e的IP地址, 這個是IPV6的地址,我們需要獲得是的IPV4的地址,所以要在上訴代碼中加一個判斷
InetAddressUtils.isIPv4Address(inetAddress.getHostAddress());
2.完整代碼如下:
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; }獲取android手機當前ip地址
public class NetWorkUtils { /** * 檢查網(wǎng)絡(luò)是否可用 * * @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的整數(shù)形式轉(zhuǎn)換成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,或者請重新打開網(wǎng)絡(luò)!/n" + ex.getMessage(); } // return null; }}Android中獲取本機ip地址和MAC地址
通過InetAddress.getLocalHost()得到始終是“127.0.0.1”,要想得到真正的網(wǎng)絡(luò)ip地址要通過下面的方法:
首先新建一個工程,修改AndroidManifest.xml文件增加用戶權(quán)限,如下:
主要函數(shù)代碼如下:
// 得到本機ip地址 public String getLocalHostIp() { String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); // 遍歷所用的網(wǎng)絡(luò)接口 while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement();// 得到每一個網(wǎng)絡(luò)接口綁定的所有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; }Android 獲取wifi的IP地址
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轉(zhuǎn)為真正的ip地址,參考的網(wǎng)上的,修改了下 private String intToIp(int i) { return (i & 0xFF)+ "." + ((i >> 8 ) & 0xFF)? + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF );} OK,這樣就好了嗎?呵呵,別忘記加上權(quán)限
總結(jié):大家可以對比一下,Android 獲取手機 IP 地址的方法,以免在編程的過程中造成不必要的問題.
新聞熱點
疑難解答