android获取网关ip
2011-04-13 14:12
274 查看
最近项目里有需求在android程序里获取系统的网关ip,最初的方案是通过
Process localProcess = Runtime.getRuntime().exec("router ")
但发现,这样子实行不通的,后来觉得麻烦,就在 android.net包里搜了一遍,让我给逮到了一个android.net.DhcpInfo,这个类里面
就存储着网关ip的数据,在此,我把获取网关ip的代码贴下来,供大家参考。
下面是获取网关的方法
由于DhcpInfo类提供的网关ip是个整数,因此还得将整数转为ip格式才可以
Process localProcess = Runtime.getRuntime().exec("router ")
但发现,这样子实行不通的,后来觉得麻烦,就在 android.net包里搜了一遍,让我给逮到了一个android.net.DhcpInfo,这个类里面
就存储着网关ip的数据,在此,我把获取网关ip的代码贴下来,供大家参考。
下面是获取网关的方法
public static String getGateWay() { if (Netgear_WifiManager.wifiManager != null) { DhcpInfo dhcpInfo=Netgear_WifiManager.wifiManager.getDhcpInfo(); Log.e("gateway is ", Netgear_IpAddressTranfer.long2ip(dhcpInfo.gateway)); } return null; }
由于DhcpInfo类提供的网关ip是个整数,因此还得将整数转为ip格式才可以
public class Netgear_IpAddressTranfer { public static int str2Ip(String ip) throws UnknownHostException { InetAddress address = InetAddress.getByName(ip);// 在给定主机名的情况下确定主机的 // IP 址。 byte[] bytes = address.getAddress();// 返回此 InetAddress 对象的原始 IP 地址 int a, b, c, d; a = byte2int(bytes[0]); b = byte2int(bytes[1]); c = byte2int(bytes[2]); d = byte2int(bytes[3]); int result = (a << 24) | (b << 16) | (c << 8) | d; return result; } public static int byte2int(byte b) { int l = b & 0x07f; if (b < 0) { l |= 0x80; } return l; } public static long ip2long(String ip) throws UnknownHostException { int ipNum = str2Ip(ip); return int2long(ipNum); } public static long int2long(int i) { long l = i & 0x7fffffffL; if (i < 0) { l |= 0x080000000L; } return l; } public static String long2ip(long ip) { int[] b = new int[4]; b[0] = (int) ((ip >> 24) & 0xff); b[1] = (int) ((ip >> 16) & 0xff); b[2] = (int) ((ip >> 8) & 0xff); b[3] = (int) (ip & 0xff); String x; x = Integer.toString(b[3]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[0]); return x; } /**** * 获取默认网关的IP地址 * * @return * @throws Exception */ public static String getDefaultGatewayIp() throws Exception { try { Process result = Runtime.getRuntime().exec("su"); BufferedReader output = new BufferedReader(new InputStreamReader( result.getInputStream())); String line = output.readLine(); while (line != null) { Log.e("new line is ", line); line = output.readLine(); } } catch (Exception e) { System.out.println(e.toString()); } return null; } }
相关文章推荐
- android获取wifi的IP,子网掩码,网关,dns等信息
- WIFI项目--Android 获取本机IP(wifi下),子网掩码,网关,dns等信息
- android获取wifi的IP,子网掩码,网关,dns等信息
- Android 手机上获取手机当前上网IP地址(手机网关给手机号分配的IP)
- android获取wifi的IP,子网掩码,网关,dns等信息
- Android笔记之获取IP、掩码、网关的有效方式
- android Ip获取过滤掉ipv6正确获取到IP
- Android shell 命令设置(获取)IP地址、网关、dns
- Android高手进阶教程(十一)之----Android 通用获取Ip的方法(判断手机是否联网的方法)!
- Android 获取本机Ip 和 Mac
- Android 局域网内功能模块开发,教你怎么快速获取局域网内所有IP并且进行通信------ MulticastSocket
- Android获取Ethernet、WIFI的ip和mac地址
- Android获取实时连接热点的设备IP
- 获取wifi的ip,子网掩码,网关,dns之类的信息
- 【小功能2】android获取手机信息(号码,内存,CPU,分辨率,MAC,IP,SD卡,IMEI,经纬度,信号强度等等)
- Android 获取当前设备外网IP以及DNS.
- 获取并修改本机的ip,网关,dns,子网掩码等信息
- 设置Android设备为自动获取IP
- VC获取本机IP、子网掩码、网关、计算广播地址等信息
- 获取当前可使用的网卡并检查网卡设置(网卡的IP,网关,子网掩码,DNS,备用DNS信息)