解决方案 »
- 新手关于R.JAVA的问题
- 如何在adb shell下设置Android的时间?
- android开发 :摄像头抓取的相片放到SD中,如果SD满了怎么办啊?
- 关于android显示子系统的认识-请指教
- 一个界面中存放两个listview的问题,求解求解。。。
- android google map开发中,怎么改变自动放大缩小控件的默认位置
- 自定义framelayout 动画在其中不能播放求解
- android中xml的解析问题
- 将应用图标下的文字过长而无法显示的文字通过自动换行显示出来
- 蓝牙打印 二维码
- 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;
}