Android - 比較版本號大小、安裝apk、獲取設備唯一標識、獲取設備mac地址

轉載請註明出處:https://blog.csdn.net/mythmayor/article/details/101023827

一、比較版本號大小

/**
  * 版本號比較:主版本號和朱版本號比較,次版本號和次版本號比較等等
  *
  * @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;
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章