一、比较版本号大小
/**
* 版本号比较:主版本号和朱版本号比较,次版本号和次版本号比较等等
*
* @param version1
* @param version2
* @return 0代表相等,1代表version1大于version2,-1代表version1小于version2
*/
public static int compareVersion(String version1, String version2) {
if (version1.equals(version2)) {
return 0;
}
String[] version1Array = version1.split("\\.");
String[] version2Array = version2.split("\\.");
int index = 0;
// 获取最小长度值
int minLen = Math.min(version1Array.length, version2Array.length);
int diff = 0;
// 循环判断每位的大小
while (index < minLen
&& (diff = Integer.parseInt(version1Array[index])
- Integer.parseInt(version2Array[index])) == 0) {
index++;
}
if (diff == 0) {
// 如果位数不一致,比较多余位数
for (int i = index; i < version1Array.length; i++) {
if (Integer.parseInt(version1Array[i]) > 0) {
return 1;
}
}
for (int i = index; i < version2Array.length; i++) {
if (Integer.parseInt(version2Array[i]) > 0) {
return -1;
}
}
return 0;
} else {
return diff > 0 ? 1 : -1;
}
}
二、安装apk
/**
* 安装APK
*
* @param context
* @param filePath
*/
public static void installApk(Context context, String filePath) {
try {
/**
* provider
* 处理android 7.0 及以上系统安装异常问题
*/
File file = new File(filePath);
Intent install = new Intent();
install.setAction(Intent.ACTION_VIEW);
install.addCategory(Intent.CATEGORY_DEFAULT);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(context, "包名.fileprovider", file);//在AndroidManifest中的android:authorities值
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
context.startActivity(install);
} catch (Exception e) {
//TODO 文件解析失败
}
}
三、获取设备唯一标识
说明:这里我们并没有获取IMEI,因为如果获取IMEI的值需要动态申请获取设备状态的权限,对于用户来说成本比较高。另外,之前在使用一个定制设备的时候发现该设备并没有IMEI值,获取时会一直报错。
/**
* 获取设备唯一标识,将ANDROID_ID与序列号拼接后转成MD5格式进行返回
*
* @param context 上下文
* @return 设备唯一标识
*/
public static String getUniqueId(Context context) {
String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
String id = androidID + Build.SERIAL;
try {
return toMD5(id);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return id;
}
}
/**
* 将字符串转为MD5
*
* @param text 要转换的字符串
* @return 转换后的字符串
* @throws NoSuchAlgorithmException
*/
private static String toMD5(String text) throws NoSuchAlgorithmException {
//获取摘要器 MessageDigest
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
//通过摘要器对字符串的二进制字节数组进行hash计算
byte[] digest = messageDigest.digest(text.getBytes());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < digest.length; i++) {
//循环每个字符 将计算结果转化为正整数;
int digestInt = digest[i] & 0xff;
//将10进制转化为较短的16进制
String hexString = Integer.toHexString(digestInt);
//转化结果如果是个位数会省略0,因此判断并补0
if (hexString.length() < 2) {
sb.append(0);
}
//将循环结果添加到缓冲区
sb.append(hexString);
}
//返回整个结果
return sb.toString();
}
四、获取设备mac地址
/**
* 获取设备MAC地址
* 需要添加的权限:INTERNET、ACCESS_WIFI_STATE
*
* @return 设备的Mac地址
*/
public static String getMacAddress() {
String macAddress = "";
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface;
try {
networkInterface = NetworkInterface.getByName("eth1");
if (networkInterface == null) {
networkInterface = NetworkInterface.getByName("wlan0");
}
if (networkInterface == null) {
macAddress = "02:00:00:00:00:02";
} else {
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
macAddress = buf.toString();
}
}
} catch (SocketException e) {
e.printStackTrace();
macAddress = "02:00:00:00:00:02";
}
return macAddress;
}