获取本机WIFI
private
String getLocalIpAddress() { WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); // 获取32位整型IP地址 int ipAddress = wifiInfo.getIpAddress(); //返回整型地址转换成“*.*.*.*”地址 return String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)); }

     
3G网络IP
public static String getIpAddress() {
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) {            e.printStackTrace();        }        return null;    }

public String getLocalIpAddress() {String ipAddress = null;try {List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());for (NetworkInterface iface : interfaces) {if (iface.getDisplayName().equals("eth0")) {List<InetAddress> addresses = Collections.list(iface.getInetAddresses());for (InetAddress address : addresses) {if (address instanceof Inet4Address) {ipAddress = address.getHostAddress();}}} else if (iface.getDisplayName().equals("wlan0")) {List<InetAddress> addresses = Collections.list(iface.getInetAddresses());for (InetAddress address : addresses) {if (address instanceof Inet4Address) {ipAddress = address.getHostAddress();}}}}} catch (SocketException e) {e.printStackTrace();}return ipAddress;}

  

需添加如下权限:

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>



更多相关文章

  1. android如何往SDCard中存取图片
  2. android 设置静态wifi地址
  3. 『ANDROID』Android中的onWindowFocusChanged()方法详解
  4. android获取屏幕宽高的两种方法
  5. Android(安卓)获取设备宽高分辨率
  6. Read Contacts information in Android(安卓)system.
  7. Android实用代码片段(一)
  8. android C++ 和 jni,根据JNIEnv的FindClass获取java类,包括多线程
  9. Android(安卓)获取几天后的时间

随机推荐

  1. 我的Android进阶之旅------>Android中and
  2. Android数据库事务浅析
  3. AIDL (Android Interface Definition Lan
  4. Android在TQ2440开发板上的移植(转)
  5. android mediaplayer 播放 视频 【转】
  6. Android Handler机制 - handleMessage究
  7. KJFrameForAndroid
  8. Android软件如何自动升级
  9. Android自定义属性时TypedArray的使用方
  10. 使用Docker编译Android系统内核