public class GetIPAddr { public String getWifiIPAddress(boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; } public String getPingIPAddress(String domain) { String ipaddress = null; // dip = (EditText) findViewById(R.id.domain_name); // String oreIP = dip.getText().toString(); String oreIP = domain; try { Process p = Runtime.getRuntime().exec("ping -c 1 " + oreIP); int status = p.waitFor(); InputStream input = p.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(input)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } String returnMsg = buffer.toString(); int po1 = returnMsg.indexOf('(') + 1; int po2 = returnMsg.indexOf(')'); ipaddress = returnMsg.substring(po1, po2); } catch (IOException e) { return ""; } catch (InterruptedException e) { return ""; } return ipaddress; } }
2015年1月12日 星期一
use ping to get ip & get wifi ip
標籤:
Android apk
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言