一、介紹
AndroidUtilCode 是一款收錄了大量常用工具類的 Android 庫,全稱為 Android Util Code,是 Android 平台上的工具類庫。這個庫里包含了 Android 開發過程中經常使用的工具方法,從開發效率、代碼重用性、代碼可讀性等方面都具有很大的幫助。另外,AndroidUtilCode 最獨到的就是將多種功能進行了封裝,對 Android 開發中使用頻率十分高的工具類做了很好的整合。
二、功能介紹
AndroidUtilCode 涵蓋的功能之多且發雜,無法完全列舉,主要包括Android框架相關、App相關、Bar相關、設備相關、Encode、File、Image、Keyboard、Lang、Log、Meta、Network、Permission、Reflect、Regex、Shell相關、Size、SPCache、Sub-process、Thread相關、Time、Toast、Zip等方面的功能。
1. Android框架相關
提供了對 Android 框架相關信息的獲取和使用,例如獲取 App 版本號、獲取設備 UID 等。
public static String getAppVersionName(Context context) // 獲取 App 版本號
public static int getAppVersionCode(Context context) // 獲取 App 版本碼
public static String getAndroidID() // 獲取 Android 設備唯一標識
public static String getMacAddress(Context context) // 獲取 Mac 地址
public static String getManufacturer() //獲取設備廠商
public static String getModel() // 獲取設備型號
public static String getDevice() //獲取設備名
2. App相關
提供了對 App 相關信息的獲取和使用,例如獲取當前 App 包名、安裝 App、App 啟動時間等。
public static void installApp(String filePath) // 安裝 App
public static boolean isAppInstalled(String packageName) // 判斷 App 是否已安裝
public static String getAppPackageName() // 獲取當前 App 包名
public static long getAppLastInstallTime(Context context) // 獲取 App 最後一次安裝時間
public static long getAppFirstInstallTime(Context context) // 獲取 App 第一次安裝時間
public static long getAppLastUpdateTime(Context context) // 獲取 App 最後一次更新時間
public static long getAppVersionCode(Context context) // 獲取 App 版本碼
public static String getAppVersionName(Context context) // 獲取 App 版本號
public static long getAppSize(Context context) // 獲取 App 安裝包大小
public static boolean isAppDebug() // 判斷 App 是否 Debug 版本
public static boolean isAppSystem() // 判斷 App 是否為系統應用
public static boolean isAppForeground() // 判斷 App 是否在前台
public static void relaunchApp() // 重啟 App
public static void exitApp() // 退出 App
public static void shareText(String text, String title) // 分享文本
public static void shareImage(String filePath, String title) // 分享圖片
public static void shareVideo(String filePath, String title) // 分享視頻
public static void shareAudio(String filePath, String title) // 分享音頻
public static void shareFile(String filePath, String title) // 分享文件
3. Bar相關
提供了對 Bar 相關信息的獲取和使用,例如設置狀態欄顏色和透明度。
public static void setStatusBarColor(Activity activity, int color) // 設置狀態欄顏色
public static void setStatusBarColor(Activity activity, int color, boolean isDecor) //設置狀態欄顏色,使用時不會影響其他 View 的布局
public static void setStatusBarAlpha(Activity activity, int alpha) // 設置狀態欄透明度
public static void setStatusBarAlpha(Activity activity, int alpha, boolean isDecor) // 設置狀態欄透明度,使用時不會影響其他 View 的布局
public static void setStatusBarColor4Drawer(Activity activity, DrawerLayout drawer, Toolbar toolbar, int color) // 為 DrawerLayout 設置狀態欄顏色
public static void setStatusBarAlpha4Drawer(Activity activity, DrawerLayout drawer, Toolbar toolbar, int alpha) // 為 DrawerLayout 設置狀態欄透明度
4. 設備相關
提供了對設備相關信息的獲取和使用,例如獲取設備屏幕信息、獲取設備生產商、獲取設備型號、獲取設備寬高、獲取設備序列號等。
public static boolean isDeviceRooted() // 判斷設備是否有 ROOT 許可權
public static String getSDKVersionName() // 獲取 Android SDK 版本號
public static int getSDKVersionCode() // 獲取 Android SDK 版本碼
public static int getMinSdkVersion(Context context) // 獲取 App 最低版本號
public static int getMaxSdkVersion(Context context) // 獲取 App 最高版本號
public static String getSDKVersion() // 獲取 Android SDK 版本
public static String getFingerprint() // 獲取設備指紋信息
public static String getBoard() // 獲取設備主板信息
public static String getBootLoader() // 獲取設備引導程序版本號
public static String getBrand() // 獲取設備品牌信息
public static String getDevice() // 獲取設備型號信息
public static String getDisplay() // 獲取設備顯示信息
public static String getHardware() // 獲取設備硬體信息
public static String getManufacturer() // 獲取設備製造商信息
public static String getModel() // 獲取手機型號信息
public static String getProduct() // 獲取設備驅動信息
public static String getSerial() // 獲取設備序列號(該序列號不同於 AndroidId)
public static String getAndroidId() // 獲取 AndroidId
public static String getMacAddress() // 獲取 Mac 地址
public static String getIPAddress(boolean useIPv4) // 獲取 IP 地址
public static String getIMEI(Context context) // 獲取設備 IMEI 碼
public static String getIMSI(Context context) // 獲取設備 IMSI 碼
public static String getPhoneNumber(Context context) // 獲取設備手機號
public static int getScreenDensity() // 獲取設備屏幕密度(DPI)
public static float getScreenDensityScale() // 獲取設備屏幕密度比率
public static float getScreenScale() // 獲取設備屏幕比例
public static int getScreenWidth() // 獲取設備屏幕寬度
public static int getScreenHeight() // 獲取設備屏幕高度
5. Encode
提供了對編碼相關信息的獲取和使用,例如 MD5 加密、Base64 編碼解碼等。
public static byte[] hexString2Bytes(String hexString) // 十六進位字元串轉為 byte 數組
public static String bytes2HexString(byte[] bytes) // byte 數組轉為十六進位字元串
public static String bytes2Base64(byte[] bytes) // byte 數組轉為 Base64 編碼字元串
public static byte[] base642Bytes(String base64) // Base64 編碼字元串轉為 byte 數組(可以用於圖片Base64編碼)
public static String encodeMD2ToString(String data) // MD2 加密字元串
public static String encodeMD5ToString(String data) // MD5 加密字元串
public static String encodeSHA1ToString(String data) // SHA1 加密字元串
public static String encodeSHA256ToString(String data) // SHA256 加密字元串
public static String encodeSHA512ToString(String data) // SHA512 加密字元串
public static String encodeHmacMD5ToString(String data, String key) // HmacMD5 加密字元串
public static String encodeHmacSHA1ToString(String data, String key) // HmacSHA1 加密字元串
public static String encodeHmacSHA256ToString(String data, String key) // HmacSHA256 加密字元串
public static String encodeHmacSHA512ToString(String data, String key) // HmacSHA512 加密字元串
6. File
提供了對文件讀取和寫入、文件操作的封裝,例如獲取文件後綴名、獲取文件大小、比較文件內容是否一致等。
public static boolean isFileExists(String filePath) // 判斷文件是否存在
public static boolean writeFileFromString(String filePath, String content, boolean append) // 將字元串寫入文件
public static byte[] readFile2Bytes(String filePath) // 讀取指定文件內的 byte 數組
public static String readFile2String(String filePath) // 讀取指定文件內的字元串
public static boolean isFileEquals(File file1, File file2) // 判斷兩個文件是否相等
public static boolean isFileEquals(String filePath1, String filePath2) // 判斷兩個文件是否相等
public static boolean copyFile(String sourceFilePath, String destFilePath) // 將原文件複製到目標文件
public static boolean copyDir(String sourceDirPath, String destDirPath) // 將原文件夾複製到目標文件夾
public static boolean moveFile(String sourceFilePath, String destFilePath) // 將原文件移動到目標文件
public static boolean moveDir(String sourceDirPath, String destDirPath) // 將原文件夾移動到目標文件夾
public static boolean deleteFile(String filePath) // 刪除文件
public static boolean deleteDir(String dirPath) // 刪除文件夾
public static long getFileSize(String filePath) // 獲取文件大小
public static long getDirSize(String dirPath) // 獲取文件夾大小
public static String[] getDirName(String dirPath) // 獲取文件夾名稱
public static String[] getDirFiles(String dirPath) // 獲取文件夾內的文件名數組
public static String[] getDirPaths(String dirPath) // 獲取文件夾內的文件路徑數組
public static String getFileName(String filePath) // 獲取文件名
public static String getFileNameWithSuffix(String filePath) // 獲取文件名(帶後綴)
public static String getFileSuffix(String filePath) // 獲取文件後綴名
public static String getDirName(String filePath) // 獲取文件所在文件夾的名稱
public static String getAbsolutePath(File file) // 獲取文件絕對路徑
7. Image
提供了對圖片處理的封裝,例如圖片壓縮、圖片轉換等。
public static boolean save(Bitmap src, String filePath, Bitmap.CompressFormat format, boolean recycle) // 保存圖片
public static Bitmap compressByScale(Bitmap src, float scale, float quality, Bitmap.Config config, boolean recycle) // 按比例縮小圖片尺寸
public static Bitmap compressByScale(Bitmap src, float scale, Bitmap.Config config, boolean recycle) // 按比例縮小圖片尺寸
public static Bitmap compressByQuality(Bitmap src, int quality, Bitmap.Config config, boolean recycle) // 按質量壓縮圖片
public static Bitmap compressBySampleSize(Bitmap src, int sampleSize, Bitmap.Config config, boolean recycle) // 按採樣率縮小圖片尺寸
public static Bitmap rotate(Bitmap src, int degrees, float px, float py, boolean recycle) // 旋轉圖片
public static Bitmap rotate(Bitmap src, int degrees, boolean recycle) // 旋轉圖片
public static Bitmap toBitmap(Drawable drawable, int width, int height, Bitmap.Config config) // Drawable 轉為 Bitmap
public static byte[] bitmap2Bytes(Bitmap src, Bitmap.CompressFormat format) // Bitmap 轉為 byte 數組
public static Bitmap bytes2Bitmap(byte[] bytes) // Byte 數組轉為 Bitmap
public static Bitmap drawable2Bitmap(Drawable drawable) // Drawable 轉為 Bitmap
public static byte[] drawable2Bytes(Drawable drawable, Bitmap.CompressFormat format) // Drawable 轉為 byte 數組
public static Drawable bytes2Drawable(byte[] bytes) // Byte 數組轉為 Drawable
public static Drawable bitmap2Drawable(Bitmap bitmap) // Bitmap 轉為 Drawable
public static byte[] bitmap2Bytes(Bitmap bitmap) // Bitmap 轉為 byte 數組
public static Bitmap file2Bitmap(String filePath) // 文件轉為 Bitmap
public static Drawable file2Drawable(String filePath) // 文件轉為 Drawable
public static boolean save(Bitmap src, String filePath, Bitmap.CompressFormat format, boolean recycle) // 保存圖片
public static int getImageWidth(String filePath) // 獲取圖片寬度
public static int getImageHeight(String filePath) // 獲取圖片高度
public static String compressBySampleSize(String srcFilePath, String destFilePath, int inSampleSize) // 通過採樣率壓縮圖片
public static String compressByQuality(String srcFilePath, String destFilePath, int quality) // 通過質量壓縮圖片
public static String compressByScale(String srcFilePath, String destFilePath, float scale) // 通過縮放比例壓縮圖片
public static String compressByMaxSize(String srcFilePath, String destFilePath, int maxWidth, int maxHeight) // 通過最大寬高壓縮圖片
public static String compressByMinSize原創文章,作者:VHFIE,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/369523.html