1、WIFI情况下获取网关:
这种情况很简单,也就是通过DhcpInfo直接获取
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo info=wifiManager..getDhcpInfo();
int gateway=info.gateway;
String ip=intToIp(gateway);
/**
* int值转换为ip
* @param addr
* @return
*/
public static String intToIp(int addr) {
return ((addr & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF));
}
2、有线网络下的DHCP模式连接:通过getprop命令获取
/**
* 根据adb shell命令获取getprop中的信息(WIFI或dhcp下)
* @param str
* @return
*/
public String getGateway() {
BufferedReader bufferedReader = null;
String str2 = "