TrineaAndroidCommon 是一個整理了 Android 開發中高頻工具類的開源庫,大部分同樣適用於純 Java 專案。原始碼託管於 GitHub,支援直接作為 Library 依賴或按需複製代碼。
| 工具類 | 核心功能 | 代表方法 |
|---|---|---|
| HttpUtils | HTTP 請求封裝,支援 GET/POST、參數配置、GZIP、緩衝讀取 | httpGet(url), httpPost(url, params), httpGetString(url) |
| DownloadManagerPro | 系統 DownloadManager 增強,查詢進度、狀態、錯誤碼、檔案路徑 | getStatusById(id), getDownloadBytes(id), getFileName(id) |
| ShellUtils | Shell 命令執行,Root 權限檢查、靜默安裝 APK | checkRootPermission(), execCommand(cmd, isRoot) |
| PackageUtils | 應用安裝/解除安裝、系統應用判斷、Root 靜默操作 | install(ctx, apkPath), uninstall(ctx, pkg), isSystemApplication(ctx, pkg) |
| PreferencesUtils | SharedPreferences 讀寫封裝,支援 String/Int/Boolean/Float/Long/Set | putString(ctx, key, val), getString(ctx, key) |
| JSONUtils | JSON 物件安全取值,預設值回退 | getString(json, key, def), getMap(json, key) |
| FileUtils | 檔案讀寫、大小、刪除、複製、MD5、MIME 類型 | readFile(path), writeFile(path, content, append), getFileSize(path) |
| ResourceUtils | 從 assets/raw 讀取資源檔內容 | geFileFromAssets(ctx, fileName), geFileFromRaw(ctx, resId) |
| StringUtils | 常用字串判斷、編碼、首字母大寫、截斷 | isEmpty(str), isBlank(str), utf8Encode(str), capitalizeFirstLetter(str) |
| ParcelUtils | Parcel 讀寫 Boolean、Map、List 等特殊類型 | readBoolean(parcel), writeHashMap(map, parcel) |
| RandomUtils | 隨機字串、數字、顏色、UUID | getRandom(chars, len), getRandomNumbers(len) |
| ArrayUtils | 陣列判空、取前後元素、循環取值 | isEmpty(arr), getNext(arr, val, def, circle) |
| ImageUtils | Bitmap/Drawable/ByteArray 互轉、縮放 | bitmapToDrawable(bmp), scaleImage(bmp, sw, sh) |
| ListUtils | List 判空、Join、去重添加 | isEmpty(list), join(list, sep), addDistinctEntry(list, entry) |
| MapUtils | Map 判空、字串解析為 Map、Map 轉 JSON | isEmpty(map), parseKeyAndValueToMap(str, ...), toJson(map) |
| ObjectUtils | 物件比較、轉換(Integer[] ↔ int[]) | isEquals(a, b), compare(v1, v2), transformIntArray(arr) |
| SerializeUtils | 物件序列化到檔案、反序列化 | serialization(path, obj), deserialization(path) |
| SystemUtils | 系統資訊:建議執行緒池大小 | getDefaultThreadPoolSize() |
| TimeUtils | 時間格式化、當前時間戳、相對時間 | getCurrentTimeInLong(), getTime(millis, format) |
// build.gradle
dependencies {
implementation 'com.trinea.android:common:4.2.0' // 具體版本請查 Maven Central
}
// 或複製源碼到專案 module
// HttpUtils
String html = HttpUtils.httpGetString("https://api.example.com/data");
// FileUtils
String json = FileUtils.readFile("/sdcard/config.json");
FileUtils.writeFile("/sdcard/log.txt", "new line\n", true);
// TimeUtils
String now = TimeUtils.getTime(System.currentTimeMillis(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// StringUtils
if (StringUtils.isBlank(input)) { ... }
String encoded = StringUtils.utf8Encode("中文");
TrineaAndroidCommon 將 Android 開發中分散在各專案的「工具方法」標準化、模組化,極大減少重複造輪子。建議團隊專案統一引入,並根據業務需求擴充自有工具類。