主要介紹總結(jié)的Android開發(fā)中常用的工具類,大部分同樣適用于Java。
目前包括HttpUtils、DownloadManagerPro、ShellUtils、PackageUtils、PreferencesUtils、JSONUtils、FileUtils、ResourceUtils、StringUtils、ParcelUtils、RandomUtils、ArrayUtils、ImageUtils、ListUtils、MapUtils、ObjectUtils、SerializeUtils、SystemUtils、TimeUtils。
The English version of this article see:Android Common Utils
所有代碼都在TrineaAndroidCommon@Github中,歡迎Star或Fork^_*,除這些工具類外此項(xiàng)目還包括緩存、下拉ListView等。詳細(xì)接口介紹可見TrineaAndroidCommon API Guide。
具體使用:可直接引入TrineaAndroidCommon作為你項(xiàng)目的library(如何拉取代碼及添加公共庫(kù)),或是自己抽取其中的部分使用。
1、HttpUtils
Http網(wǎng)絡(luò)工具類,主要包括httpGet、httpPost以及http參數(shù)相關(guān)方法,以httpGet為例:
static HttpResponse httpGet(HttpRequest request)
static HttpResponse httpGet(java.lang.String httpUrl)
static String httpGetString(String httpUrl)
包含以上三個(gè)方法,默認(rèn)使用gzip壓縮,使用bufferedReader提高讀取速度。
HttpRequest中可以設(shè)置url、timeout、userAgent等其他http參數(shù)
HttpResponse中可以獲取返回內(nèi)容、http響應(yīng)碼、http過期時(shí)間(Cache-Control的max-age和expires)等
前兩個(gè)方法可以進(jìn)行高級(jí)參數(shù)設(shè)置及豐富內(nèi)容返回,第三個(gè)方法可以簡(jiǎn)單的傳入url獲取返回內(nèi)容,httpPost類似。更詳細(xì)的設(shè)置可以直接使用HttpURLConnection或apache的HttpClient。
源碼可見HttpUtils.java,更多方法及更詳細(xì)參數(shù)介紹可見HttpUtils Api Guide。
2、DownloadManagerPro
Android系統(tǒng)下載管理DownloadManager增強(qiáng)方法,可用于包括獲取下載相關(guān)信息,如:
getStatusById(long) 得到下載狀態(tài)
getDownloadBytes(long) 得到下載進(jìn)度信息
getBytesAndStatus(long) 得到下載進(jìn)度信息和狀態(tài)
getFileName(long) 得到下載文件路徑
getUri(long) 得到下載uri
getReason(long) 得到下載失敗或暫停原因
getPausedReason(long) 得到下載暫停原因
getErrorCode(long) 得到下載錯(cuò)誤碼
源碼可見DownloadManagerPro.java,更多方法及更詳細(xì)參數(shù)介紹可見DownloadManagerPro Api Guide。關(guān)于Android DownManager使用可見DownManager Demo。
3、ShellUtils
Android Shell工具類,可用于檢查系統(tǒng)root權(quán)限,并在shell或root用戶下執(zhí)行shell命令。如:
checkRootPermission() 檢查root權(quán)限
execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) shell環(huán)境執(zhí)行命令,第二個(gè)參數(shù)表示是否root權(quán)限執(zhí)行
execCommand(String command, boolean isRoot) shell環(huán)境執(zhí)行命令
源碼可見ShellUtils.java,更多方法及更詳細(xì)參數(shù)介紹可見ShellUtils Api Guide。關(guān)于靜默安裝可見apk-root權(quán)限靜默安裝。
4、PackageUtils
Android包相關(guān)工具類,可用于(root)安裝應(yīng)用、(root)卸載應(yīng)用、判斷是否系統(tǒng)應(yīng)用等,如:
install(Context, String) 安裝應(yīng)用,如果是系統(tǒng)應(yīng)用或已經(jīng)root,則靜默安裝,否則一般安裝
uninstall(Context, String) 卸載應(yīng)用,如果是系統(tǒng)應(yīng)用或已經(jīng)root,則靜默卸載,否則一般卸載
isSystemApplication(Context, String) 判斷應(yīng)用是否為系統(tǒng)應(yīng)用
源碼可見PackageUtils.java,更多方法及更詳細(xì)參數(shù)介紹可見ShellUtils Api Guide。關(guān)于靜默安裝可見apk-root權(quán)限靜默安裝。
5、PreferencesUtils
Android SharedPreferences相關(guān)工具類,可用于方便的向SharedPreferences中讀取和寫入相關(guān)類型數(shù)據(jù),如:
putString(Context, String, String) 保存string類型數(shù)據(jù)
putInt(Context, String, int) 保存int類型數(shù)據(jù)
getString(Context, String) 獲取string類型數(shù)據(jù)
getInt(Context, String) 獲取int類型數(shù)據(jù)
可通過修改PREFERENCE_NAME變量修改preference name
源碼可見PreferencesUtils.java,更多方法及更詳細(xì)參數(shù)介紹可見PreferencesUtils Api Guide。
6、JSONUtils
JSONUtils工具類,可用于方便的向Json中讀取和寫入相關(guān)類型數(shù)據(jù),如:
String getString(JSONObject jsonObject, String key, String defaultValue) 得到string類型value
String getString(String jsonData, String key, String defaultValue) 得到string類型value
表示從json中讀取某個(gè)String類型key的值
getMap(JSONObject jsonObject, String key) 得到map
getMap(String jsonData, String key) 得到map
表示從json中讀取某個(gè)Map類型key的值
源碼可見JSONUtils.java,更多方法及更詳細(xì)參數(shù)介紹可見JSONUtils Api Guide。
7、FileUtils
文件工具類,可用于讀寫文件及對(duì)文件進(jìn)行操作。如:
readFile(String filePath) 讀文件
writeFile(String filePath, String content, boolean append) 寫文件
getFileSize(String path) 得到文件大小
deleteFile(String path) 刪除文件
源碼可見FileUtils.java,更多方法及更詳細(xì)參數(shù)介紹可見FileUtils Api Guide。
8、ResourceUtils
Android Resource工具類,可用于從android資源目錄的raw和assets目錄讀取內(nèi)容,如:
geFileFromAssets(Context context, String fileName) 得到assets目錄下某個(gè)文件內(nèi)容
geFileFromRaw(Context context, int resId) 得到raw目錄下某個(gè)文件內(nèi)容
源碼可見ResourceUtils.java,更多方法及更詳細(xì)參數(shù)介紹可見ResourceUtils Api Guide。
9、StringUtils
String工具類,可用于常見字符串操作,如:
isEmpty(String str) 判斷字符串是否為空或長(zhǎng)度為0
isBlank(String str) 判斷字符串是否為空或長(zhǎng)度為0 或由空格組成
utf8Encode(String str) 以u(píng)tf-8格式編碼
capitalizeFirstLetter(String str) 首字母大寫
源碼可見StringUtils.java,更多方法及更詳細(xì)參數(shù)介紹可見StringUtils Api Guide。
10、ParcelUtils
新聞熱點(diǎn)
疑難解答
圖片精選