Android获取移动设备IP地址
2013-08-01 11:08
351 查看
MainActivity如下:
main.xml如下:
package cn.testip; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 获取移动设备的IP地址 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String IP=getIP(); System.out.println("The IP of this handset is : "+IP); } private String getIP() { String IP = null; StringBuilder IPStringBuilder = new StringBuilder(); try { Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); while (networkInterfaceEnumeration.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); while (inetAddressEnumeration.hasMoreElements()) { InetAddress inetAddress = inetAddressEnumeration.nextElement(); if (!inetAddress.isLoopbackAddress()&& !inetAddress.isLinkLocalAddress()&& inetAddress.isSiteLocalAddress()) { IPStringBuilder.append(inetAddress.getHostAddress().toString()+"\n"); } } } } catch (SocketException ex) { } IP = IPStringBuilder.toString(); return IP; } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取设备的IP" android:layout_centerInParent="true" android:textSize="28sp" /> </RelativeLayout>
相关文章推荐
- Android获取移动设备IP地址
- Android获取移动设备的IP地址
- Android获取当前设备有效的IP地址
- Android移动开发-获取Android移动设备上支持的传感器的实现
- android获取设备eth0,eth1,wlan0的IP地址
- 获取android设备的IP地址
- 移动设备IP地址的获取
- Android获取设备的IP地址的两种方法
- [Android]获取设备IP地址
- Android 获取当前设备的IP地址
- android 获取设备信息的IP地址和Mac地址—亲测无误!!
- 如何获取Android设备的IP地址
- 获取android手机移动设备号
- 如何获取Android移动终端设备唯一ID
- Android获取设备ip地址和Mac地址
- 同wifi环境下android设备自动获取socket服务端的ip地址和端口号的方法-UDP广播
- 获取PC或移动设备的所有IP地址
- android 获取当前设备的ip地址 mac地址 设备号 手机号 运营商信息
- Android获取局域网所有设备的ip地址
- 如何在android 中获取Wifi设备的IP地址