在android程序里获取系统的网关ip,
使用android.net.DhcpInfo,这个类里面就存储着网关ip的数据,
下面是获取网关的方法
public static String getGateWay() {
if (Netgear_WifiManager.wifiManager != null){
DhcpInfodhcpInfo=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) throwsUnknownHostException {
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) throwsUnknownHostException {
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地址
public static String getDefaultGatewayIp()throws Exception {
try {
Process result =Runtime.getRuntime().exec("su");
BufferedReader output = new BufferedReader(newInputStreamReader( 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网络信息android获取wifi信息源码:
public class Main extends Activity {
private TextView tv;
private WifiManager wifiManager;
private DhcpInfo dhcpInfo;
private WifiInfo wifiInfo;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
dhcpInfo =wifiManager.getDhcpInfo();
wifiInfo =wifiManager.getConnectionInfo();
int ip =wifiInfo.getIpAddress();
int speed =wifiInfo.getLinkSpeed();
int networkId =wifiInfo.getNetworkId();
int getRssi =wifiInfo.getRssi();
String macAddr =wifiInfo.getMacAddress();
String getSSID =wifiInfo.getSSID();
String detail =wifiInfo.toString();
String bssid =wifiInfo.getBSSID();
tv = (TextView)this.findViewById(R.id.wifiInfo);
tv.append("ip :" + ip +"n");
tv.append("speed :" + speed +"n");
tv.append("macAddr :" + macAddr+"n");
tv.append("networkId :" +networkId+ "n");
tv.append("getRssi :" + getRssi+"n");
tv.append("getSSID :" + getSSID+"n");
tv.append("detail :" + detail +"n");
tv.append("bssid :" + bssid +"n");
tv.append("dhcpInfo geteway is:"+ dhcpInfo.gateway + "n");
tv.append("dhcpInfo mask is :"+ dhcpInfo.netmask + "n");
tv.append("dhcpInfo ip is :" +dhcpInfo.ipAddress + "n");
tv.append("ip is :" +FormatIP(ip) +"n");
tv.append("geteway is :" +FormatIP(dhcpInfo.gateway) + "n");
tv.append("mask is :" +FormatIP(dhcpInfo.netmask) +"n");
}
// IP地址转化为字符串格式
public String FormatIP(int IpAddress) {
returnFormatter.formatIpAddress(IpAddress);
}
}
在anroidMainfest.xml加入权限<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>