app开发,难免会遇到需要获取设备信息,作为标示,使用比较多的,那肯定是mac或者设备的ip地址来标识当前使用的用户。这些信息也经常会出现在网络请求的请求头里面。
1.获取设备的mac地址
/**
* 获取设备mac地址
*
* @return
*/
public static String getDeviceIDByMac(Context context) {
String wifiMac = null;
//获取有线的mac地址
if (TextUtils.isEmpty(wifiMac)) {
try {
return loadFileAsString("/sys/class/net/eth0/address")
.toLowerCase().substring(0, 17);
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
//获取无线的mac地址
if (TextUtils.isEmpty(wifiMac)) {
try {
Process pp = Runtime.getRuntime().exec(
"cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = null;
while ((line = input.readLine()) != null) {
wifiMac = line.trim();
break;
}
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
if (TextUtils.isEmpty(wifiMac)) {
try {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
wifiMac = info.getMacAddress();
} catch (Exception e) {
e.printStackTrace();
}
}
return wifiMac;
}
private static String loadFileAsString(String filePath) throws IOException {
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
}
reader.close();
return fileData.toString();
}
2.获取设备的IP地址
/**
* 获取设备IP地址
* @param context:上下文对象
* @return
*/
public static String getIpAddress(Context context) {
NetworkInfo info = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info != null && info.isConnected()) {
// 3/4g网络
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
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() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
// wifi网络
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());
return ipAddress;
} else if (info.getType() == ConnectivityManager.TYPE_ETHERNET) {
// 有限网络
return getLocalIp();
}
}
return null;
}
private static String intIP2StringIP(int ip) {
return (ip & 0xFF) + "." +
((ip >> 8) & 0xFF) + "." +
((ip >> 16) & 0xFF) + "." +
(ip >> 24 & 0xFF);
}
// 获取有限网IP
private static String getLocalIp() {
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()
&& inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
}
return "0.0.0.0";
}