Android在以太网下如果获取子网掩码、默认网关、DNS啊? 以太网android子网掩码默认网关DNS 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 android 执行linux命令 试试看 目测不需要root权限 EthernetDevInfo mEthInfo = mEthManager.getSavedEthConfig(); Log.i(TAG, "mEthInfo == null : " + (mEthInfo == null)); if (mEthInfo != null) { String ipAddress = mEthInfo.getIpAddress(); String netMask = mEthInfo.getNetMask(); String dns = mEthInfo.getDnsAddr(); String gateWay = mEthInfo.getRouteAddr(); 额,linux命令的话,应该可以看到的,可是要怎么显示在android应用的界面上啊?求指点 EthernetDevInfo 这个class import android.net.ethernet.EthernetDevInfo;会提示找不到啊,我在api里面也没有找到呢,难道真的像别人说的EthernetDevInfo 在framework层?这样的话,我怎么使用呢?傻眼了、、、、 把已经得到的信息的方法附上,都是从别人那里整理的,验证过木有问题。下面附上获取mac地址,以太网下获取IP的方法以及wifi下获取网络信息的方法。获取Mac地址:public static String getMacAddress(){ try { return loadFileAsString("/sys/class/net/eth0/address") .toUpperCase().substring(0, 17); } catch (Exception e) { e.printStackTrace(); return null; } }以太网下获取IP:public static String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("WifiPreference IpAddress", ex.toString()); } return null; }wifi下获取相关网络信息:wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);d = wm.getDhcpInfo();//将地址码转换成字符串类型 public String FormatString(int value){ String strValue=""; byte[] ary =intToByteArray(value); for(int i=ary.length-1;i>=0;i--){ strValue+=(ary & 0xFF); if(i>0){ strValue+="."; } } return strValue; } public byte[] intToByteArray(int value){ byte[] b = new byte[4]; for (int i = 0; i < 4; i++){ int offset = (b.length - 1 - i) * 8; b = (byte) ((value >>> offset) & 0xFF); } return b; }IP:FormatString(d.ipAddress).toString();默认网关:FormatString(d.gateway).toString();子网掩码:FormatString(d.netmask).toString(); 不是来抬杠的,貌似你这个方法public static String getMacAddress(){并不能在所有的手机都 适用。我拿自己的手机测试了一下,并没有找到eth0这个节点。我的地址是/sys/class/net/wlan0/address 额、额、我的测试是在公司生产的平板上,不是在手机上测的,sorry,忘了说明了! 为什么我的获取 机顶盒 以太网的 ip地址,掩码,网关,DNS全为空啊!???????求解!! public String getLocalHostIp() { String ipaddress = ""; try { Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); // Traverse the network interface while (en.hasMoreElements()) { NetworkInterface nif = en.nextElement();// Each network // interface binding,all // IP Enumeration<InetAddress> inet = nif.getInetAddresses(); // Traverse each all the IP interface binding while (inet.hasMoreElements()) { InetAddress ip = inet.nextElement(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip .getHostAddress())) { Log.e("--------------------IP", ip.getHostAddress()); return ipaddress = ip.getHostAddress(); } } } } catch (SocketException e) { Log.e("---------------feige", "获取本地IP失败"); e.printStackTrace(); } return ipaddress; } 求助,怎么跳转到某一个指定的动态壁纸预览界面 ubuntu10.04安装repo时候出的问题 跪求android的wifi调试高手指点一下 怎样调用android系统自带的应用? 被虐的受不了了,特来求助 android的view.getScrollX()与scroll的getCurrX()有什么区别? 开源自己一款日历应用源码给大家 list中内容怎么替换 Listview内容动态改变后怎么重新执行layoutanimation呢 请问谁遇到过这个问题(内存泄漏) SharedPreferences 莫名消失 dev/graphics/fb0 截图出来为什么是厂商logo
Log.i(TAG, "mEthInfo == null : " + (mEthInfo == null));
if (mEthInfo != null) {
String ipAddress = mEthInfo.getIpAddress();
String netMask = mEthInfo.getNetMask();
String dns = mEthInfo.getDnsAddr();
String gateWay = mEthInfo.getRouteAddr();
额,linux命令的话,应该可以看到的,可是要怎么显示在android应用的界面上啊?求指点
下面附上获取mac地址,以太网下获取IP的方法以及wifi下获取网络信息的方法。
获取Mac地址:
public static String getMacAddress(){
try {
return loadFileAsString("/sys/class/net/eth0/address")
.toUpperCase().substring(0, 17);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}以太网下获取IP:
public static String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("WifiPreference IpAddress", ex.toString());
}
return null;
}wifi下获取相关网络信息:wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
d = wm.getDhcpInfo();//将地址码转换成字符串类型
public String FormatString(int value){
String strValue="";
byte[] ary =intToByteArray(value);
for(int i=ary.length-1;i>=0;i--){
strValue+=(ary & 0xFF);
if(i>0){
strValue+=".";
}
}
return strValue;
} public byte[] intToByteArray(int value){
byte[] b = new byte[4];
for (int i = 0; i < 4; i++){
int offset = (b.length - 1 - i) * 8;
b = (byte) ((value >>> offset) & 0xFF);
}
return b;
}IP:FormatString(d.ipAddress).toString();
默认网关:FormatString(d.gateway).toString();
子网掩码:FormatString(d.netmask).toString();
我的地址是/sys/class/net/wlan0/address
String ipaddress = ""; try {
Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces();
// Traverse the network interface
while (en.hasMoreElements()) {
NetworkInterface nif = en.nextElement();// Each network
// interface binding,all
// IP
Enumeration<InetAddress> inet = nif.getInetAddresses();
// Traverse each all the IP interface binding
while (inet.hasMoreElements()) {
InetAddress ip = inet.nextElement();
if (!ip.isLoopbackAddress()
&& InetAddressUtils.isIPv4Address(ip
.getHostAddress())) {
Log.e("--------------------IP", ip.getHostAddress());
return ipaddress = ip.getHostAddress();
}
}
}
} catch (SocketException e) {
Log.e("---------------feige", "获取本地IP失败");
e.printStackTrace();
} return ipaddress;
}