天天看點

2013最新Android常用的工具類整理

主要介紹總結的Android開發中常用的工具類,大部分同樣适用于Java。

目前包括HttpUtils、DownloadManagerPro、ShellUtils、PackageUtils、

PreferencesUtils、JSONUtils、FileUtils、ResourceUtils、StringUtils、

ParcelUtils、RandomUtils、ArrayUtils、ImageUtils、ListUtils、MapUtils、

ObjectUtils、SerializeUtils、SystemUtils、TimeUtils。

1、HttpUtils

Http網絡工具類,主要包括httpGet、httpPost以及http參數相關方法,以httpGet為例:

static HttpResponse httpGet(HttpRequest request)

static HttpResponse httpGet(java.lang.String httpUrl)

static String httpGetString(String httpUrl)

包含以上三個方法,預設使用gzip壓縮,使用bufferedReader提高讀取速度。

HttpRequest中可以設定url、timeout、userAgent等其他http參數

HttpResponse中可以擷取傳回内容、http響應碼、http過期時間等

前兩個方法可以進行進階參數設定及豐富内容傳回,第三個方法可以簡單的傳入url擷取傳回内容,httpPost類似。更詳細的設定可以直接使用HttpURLConnection或apache的HttpClient。

2、DownloadManagerPro

Android系統下載下傳管理DownloadManager增強方法,可用于包括擷取下載下傳相關資訊,如:

getStatusById(long) 得到下載下傳狀态

getDownloadBytes(long) 得到下載下傳進度資訊

getBytesAndStatus(long) 得到下載下傳進度資訊和狀态

getFileName(long) 得到下載下傳檔案路徑

getUri(long) 得到下載下傳uri

getReason(long) 得到下載下傳失敗或暫停原因

getPausedReason(long) 得到下載下傳暫停原因

getErrorCode(long) 得到下載下傳錯誤碼

3、ShellUtils

Android Shell工具類,可用于檢查系統root權限,并在shell或root使用者下執行shell指令。如:

checkRootPermission() 檢查root權限

execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) shell環境執行指令,第二個參數表示是否root權限執行

execCommand(String command, boolean isRoot) shell環境執行指令

4、PackageUtils

Android包相關工具類,可用于(root)安裝應用、(root)解除安裝應用、判斷是否系統應用等,如:

install(Context, String) 安裝應用,如果是系統應用或已經root,則靜默安裝,否則一般安裝

uninstall(Context, String) 解除安裝應用,如果是系統應用或已經root,則靜默解除安裝,否則一般解除安裝

isSystemApplication(Context, String) 判斷應用是否為系統應用

5、PreferencesUtils

Android SharedPreferences相關工具類,可用于友善的向SharedPreferences中讀取和寫入相關類型資料,如:

putString(Context, String, String) 儲存string類型資料

putInt(Context, String, int) 儲存int類型資料

getString(Context, String) 擷取string類型資料

getInt(Context, String) 擷取int類型資料

可通過修改PREFERENCE_NAME變量修改preference name

6、JSONUtils

JSONUtils工具類,可用于友善的向Json中讀取和寫入相關類型資料,如:

String getString(JSONObject jsonObject, String key, String defaultValue) 得到string類型value

String getString(String jsonData, String key, String defaultValue) 得到string類型value

表示從json中讀取某個String類型key的值

getMap(JSONObject jsonObject, String key) 得到map

getMap(String jsonData, String key) 得到map

表示從json中讀取某個Map類型key的值

7、FileUtils

檔案工具類,可用于讀寫檔案及對檔案進行操作。如:

readFile(String filePath) 讀檔案

writeFile(String filePath, String content, boolean append) 寫檔案

getFileSize(String path) 得到檔案大小

deleteFile(String path) 删除檔案

8、ResourceUtils

Android Resource工具類,可用于從android資源目錄的raw和assets目錄讀取内容,如:

geFileFromAssets(Context context, String fileName) 得到assets目錄下某個檔案内容

geFileFromRaw(Context context, int resId) 得到raw目錄下某個檔案内容

9、StringUtils

String工具類,可用于常見字元串操作,如:

isEmpty(String str) 判斷字元串是否為空或長度為0

isBlank(String str) 判斷字元串是否為空或長度為0 或由空格組成

utf8Encode(String str) 以utf-8格式編碼

capitalizeFirstLetter(String str) 首字母大寫

10、ParcelUtils

Android Parcel工具類,可用于從parcel讀取或寫入特殊類型資料,如:

readBoolean(Parcel in) 從pacel中讀取boolean類型資料

readHashMap(Parcel in, ClassLoader loader) 從pacel中讀取map類型資料

writeBoolean(boolean b, Parcel out) 向parcel中寫入boolean類型資料

writeHashMap(Map<K, V> map, Parcel out, int flags) 向parcel中寫入map類型資料

11、RandomUtils

随機數工具類,可用于擷取固定大小固定字元内的随機數,如:

getRandom(char[] sourceChar, int length) 生成随機字元串,所有字元均在某個字元串内

getRandomNumbers(int length) 生成随機數字

12、ArrayUtils

數組工具類,可用于數組常用操作,如:

isEmpty(V[] sourceArray) 判斷數組是否為空或長度為0

getLast(V[] sourceArray, V value, V defaultValue, boolean isCircle) 得到數組中某個元素前一個元素,isCircle表示是否循環

getNext(V[] sourceArray, V value, V defaultValue, boolean isCircle) 得到數組中某個元素下一個元素,isCircle表示是否循環

13、ImageUtils

圖檔工具類,可用于Bitmap, byte array, Drawable之間進行轉換以及圖檔縮放,目前功能薄弱,後面會進行增強。如:

bitmapToDrawable(Bitmap b) bimap轉換為drawable

drawableToBitmap(Drawable d) drawable轉換為bitmap

drawableToByte(Drawable d) drawable轉換為byte

scaleImage(Bitmap org, float scaleWidth, float scaleHeight) 縮放圖檔

14、ListUtils

List工具類,可用于List常用操作,如:

isEmpty(List<V> sourceList) 判斷List是否為空或長度為0

join(List<String> list, String separator) List轉換為字元串,并以固定分隔符分割

addDistinctEntry(List<V> sourceList, V entry) 向list中添加不重複元素

15、MapUtils

Map工具類,可用于Map常用操作,如:

isEmpty(Map<K, V> sourceMap) 判斷map是否為空或長度為0

parseKeyAndValueToMap(String source, String keyAndValueSeparator, String

keyAndValuePairSeparator, boolean ignoreSpace) 字元串解析為map

toJson(Map<String, String> map) map轉換為json格式

16、ObjectUtils

Object工具類,可用于Object常用操作,如:

isEquals(Object actual, Object expected) 比較兩個對象是否相等

compare(V v1, V v2) 比較兩個對象大小

transformIntArray(int[] source)  Integer 數組轉換為int數組

17、SerializeUtils

序列化工具類,可用于序列化對象到檔案或從檔案反序列化對象,如:

deserialization(String filePath) 從檔案反序列化對象

serialization(String filePath, Object obj) 序列化對象到檔案

18、SystemUtils

系統資訊工具類,可用于得到線程池合适的大小,目前功能薄弱,後面會進行增強。如:

getDefaultThreadPoolSize() 得到跟系統配置相符的線程池大小

19、TimeUtils

時間工具類,可用于時間相關操作,如:

getCurrentTimeInLong() 得到目前時間

getTime(long timeInMillis, SimpleDateFormat dateFormat) 将long轉換為固定格式時間字元串

繼續閱讀