我想这代码 公共类MainActivity扩展活动{越来越格式化自己的设备的IP地址的Android
private static final String TAG = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv= (TextView) findViewById(R.id.textView2);
String a=getLocalIpAddress();
tv.setText(a);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public 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()) {
String a= inetAddress.getHostAddress();
return a;
}
}
}
} catch (SocketException ex) {
// Log.e(tag, ex.toString());
}
return "";
}
}
这给了我IP地址就像00FF:22ff:.... ,但我希望它像192.168 .3.3 ... 格式的IP地址是贬值的 是否有任何其他方法,以便我可以得到格式的地址
你得到一个物理MAC地址,而不是IP地址。 –
你有没有看过在这里的任何十几个相同的问题的答案? – 323go
@CiaranBaselmans,不,他正在获取IPv6地址。 – 323go