试试这个代码希望它会帮助你
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 WhatIsMyIP extends Activity {
private static final String TAG = WhatIsMyIP.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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();
for (Enumeration<InetAddress> e = intf.getInetAddresses(); e.hasMoreElements();) {
InetAddress inetAddress = e.nextElement();
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().toString();
} 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();
Iterator<?> iter = s.iterator();
while (iter.hasNext()) {
buffer.append(iter.next());
if (iter.hasNext()) {
buffer.append(delimiter);
}
}
return buffer.toString();
}
private void updateTextView(String ipAddress) {
TextView textView = (TextView) findViewById(R.id.ip_address);
if (ipAddress != null) {
textView.setText(getString(R.string.ip_address) + ipAddress);
} else {
textView.setText(getString(R.string.ip_address) + getString(R.string.not_available));
}
}
}