设为首页收藏本站

Scripts 学盟

 找回密码
 加入学盟

QQ登录

只需一步,快速开始

查看: 1289|回复: 0
打印 上一主题 下一主题

Android 获取IP地址 [复制链接]

Rank: 8Rank: 8

跳转到指定楼层
1#
那个谁 发表于 2011-9-23 13:19:05 |只看该作者 |倒序浏览
  1. public String getLocalIpAddress() {
  2.     try {
  3.         for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
  4.                 en.hasMoreElements();) {
  5.             NetworkInterface intf = en.nextElement();
  6.             for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
  7.                 enumIpAddr.hasMoreElements();) {
  8.                 InetAddress inetAddress = enumIpAddr.nextElement();
  9.                 if (!inetAddress.isLoopbackAddress()) {
  10.                     return inetAddress.getHostAddress().toString();
  11.                 }
  12.             }
  13.         }
  14.     } catch (SocketException ex) {
  15.         Log.e(LOG_TAG, ex.toString());
  16.     }
  17.     return null;
  18. }
复制代码
1

查看全部评分

分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0
您需要登录后才可以回帖 登录 | 加入学盟

手机版|Scripts 学盟   |

GMT+8, 2024-5-9 12:27 , Processed in 1.056306 second(s), 12 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部