根据android-30days-apps\06day的例子,练习IP获取,List使用。IP获取可以通过两种方法,一个是枚举当前的网络设备,另一种是建立socket链接,然后再读取。使用getLocalAddress().getHostAddress(),可以或者标准的IP地址。
源代码:day02-showmyip
package com.ian.demos.ip;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ShowMyIP extends Activity {
private static final String TAG = ShowMyIP.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* use thread to read IP and update text */
new Thread(new Runnable() {
public void run() {
initUI();
}
}).run();
}
private List<String> getIpAddresses() {
List<String> ips = new ArrayList<String>();
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
Log.d(TAG, "Interface:" + intf.getName() );
for (Enumeration<InetAddress> e = intf.getInetAddresses(); e.hasMoreElements();)
{
InetAddress inetAddress = e.nextElement();
/* Returns whether this address is a loopback address or not.
* This implementation returns always false. Valid IPv4 loopback addresses
* are 127.d.d.d The only valid IPv6 loopback address is ::1.
*/
if (!inetAddress.isLoopbackAddress())
ips.add(inetAddress.getHostAddress().toString());
}
}
} catch (SocketException ex) {
Log.e(TAG, ex.toString(), ex);
}
return !ips.isEmpty() ? ips : Collections.<String> emptyList();
}
private String getSocketIPAdress() {
Socket conn = null;
String result = null;
try {
try {
conn = new Socket("www.google.com", 80);
result = conn.getLocalAddress().getHostAddress();
Log.d(TAG, "getSocketIPAdress:" + result);
} finally {
if (conn != null && !conn.isClosed())
conn.close();
}
} catch (Throwable t) {
Log.i(TAG, t.getMessage(), t);
}
return result;
}
private void initUI() {
List<String> ips = getIpAddresses();
final String ipAddress = !ips.isEmpty() ? join(ips, ", ") : getSocketIPAdress();
runOnUiThread(new Runnable() {
public void run() {
updateTextView(ipAddress);
}
});
}
private String join(Collection<?> s, String delimiter) {
StringBuffer buffer = new StringBuffer();
String ip = null;
Iterator<?> iter = s.iterator();
while (iter.hasNext()) {
ip = (String)iter.next();
Log.d(TAG, "join:" + ip);
buffer.append(ip);
if (iter.hasNext()) {
buffer.append(delimiter);
}
}
return buffer.toString();
}
private void updateTextView(String ipAddress) {
TextView textView = (TextView) findViewById(R.id.ip_address);
TextView tvConnIP = (TextView) findViewById(R.id.tvConnIP);
if (ipAddress != null) {
textView.setText(getString(R.string.ip_address) + ipAddress);
/* testing getSocketIPAdress directly */
tvConnIP.setText("Socket IP: " + getSocketIPAdress());
} else {
textView.setText(getString(R.string.ip_address) + getString(R.string.not_available));
}
}
}