获取本地版本号:
/**
* context 上下文对象
*/
public static String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return verName;
}
获取MAC地址:
/**
* context 上下文对象
*/
@SuppressLint("MissingPermission")
public static String getMacAddress(Context context) {
String mac = "02:00:00:00:00:00";
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// android 6.0之前(不包括6.0)
if (mac == null) {
return mac;
}
WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifi == null) {
return mac;
}
WifiInfo info = null;
try {
info = wifi.getConnectionInfo();
} catch (Exception e) {
e.printStackTrace();
}
if (info == null) {
return null;
}
mac = info.getMacAddress();
if (!TextUtils.isEmpty(mac)) {
mac = mac.toUpperCase(Locale.ENGLISH);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
// android 6.0(包括) ~ 7.0(不包括)
try {
mac = new BufferedReader(new FileReader(new File("/sys/class/net/wlan0/address"))).readLine();
} catch (IOException e) {
e.printStackTrace();
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// 循环遍历所有的网络接口,找到接口是wlan0 必须的权限<uses-permission android:name="android.permission.INTERNET"/>
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif: all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuffer res = new StringBuffer();
for (byte b : macBytes) {
res.append(String.format("%02X:", b));
}
if (res.length() > 0) {
res.deleteCharAt(res.length() - 1);
}
return res.toString();
}
} catch (SocketException e) {
e.printStackTrace();
}
}
return mac;
}