1. 程式人生 > >Android開發中常用的工具類

Android開發中常用的工具類

目前包括 HttpUtils、DownloadManagerPro、Safe.ijiami、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(HttpRequestrequest)

static HttpResponse httpGet(java.lang.StringhttpUrl)

static String httpGetString(String httpUrl)

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

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

HttpResponse中可以獲取返回內容、http響應碼、http過期時間(Cache-Control的max-age和expires)等

前兩個方法可以進行高階引數設定及豐富內容返回,第三個方法可以簡單的傳入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、Safe.ijiami

Android漏洞檢測工具,在線上傳Androidapk,檢測apk開發過程中產生的一些易被黑客利用、攻擊的漏洞,根據存在的漏洞提出相應的解決方案,一鍵生成詳細的分析報告,讓Android開發者可以一目瞭然的知道自己的Android應用存在的漏洞並及時採取措施保護起來。

4、ShellUtils

Android Shell工具類,可用於檢查系統root許可權,並在shell或root使用者下執行shell命令。如:checkRootPermission()檢查root許可權execCommand(String[] commands, boolean isRoot, booleanisNeedResultMsg) shell環境執行命令,第二個引數表示是否root許可權執行execCommand(String command, boolean isRoot) shell環境執行命令

5、PackageUtils

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

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

6、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

7、JSONUtils

JSONUtils工具類,可用於方便的向Json中讀取和寫入相關型別資料,如:

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

String getString(String jsonData, Stringkey, String defaultValue) 得到string型別value

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

getMap(JSONObject jsonObject, String key) 得到map

getMap(String jsonData, String key) 得到map

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

8、FileUtils

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

readFile(String filePath) 讀檔案

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

getFileSize(String path) 得到檔案大小

deleteFile(String path) 刪除檔案

9、ResourceUtils

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

geFileFromAssets(Context context, StringfileName) 得到assets目錄下某個檔案內容

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

10、StringUtils

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

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

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

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

capitalizeFirstLetter(String str) 首字母大寫

11、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 map, Parcel out, intflags) 向parcel中寫入map型別資料

12、RandomUtils

隨機數工具類,可用於獲取固定大小固定字元內的隨機數,如:

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

getRandomNumbers(int length) 生成隨機數字

13、ArrayUtils

陣列工具類,可用於陣列常用操作,如:

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

getLast(V[] sourceArray, V value, VdefaultValue, boolean isCircle) 得到陣列中某個元素前一個元素,isCircle表示是否迴圈

getNext(V[] sourceArray, V value, VdefaultValue, boolean isCircle) 得到陣列中某個元素下一個元素,isCircle表示是否迴圈

14、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) 縮放圖片

15、ListUtils

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

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

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

addDistinctEntry(List sourceList, V entry) 向list中新增不重複元素

16、MapUtils

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

isEmpty(Map sourceMap) 判斷map是否為空或長度為0

parseKeyAndValueToMap(String source, StringkeyAndValueSeparator, String keyAndValuePairSeparator, boolean ignoreSpace) 字串解析為map

toJson(Map map) map轉換為json格式

17、ObjectUtils

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

isEquals(Object actual, Object expected) 比較兩個物件是否相等

compare(V v1, V v2) 比較兩個物件大小

transformIntArray(int[] source) Integer 陣列轉換為int陣列

18、SerializeUtils

序列化工具類,可用於序列化物件到檔案或從檔案反序列化物件,如:

deserialization(String filePath) 從檔案反序列化物件

serialization(String filePath, Object obj) 序列化物件到檔案

19、SystemUtils

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

getDefaultThreadPoolSize() 得到跟系統配置相符的執行緒池大小

20、TimeUtils

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

getCurrentTimeInLong() 得到當前時間

getTime(long timeInMillis, SimpleDateFormatdateFormat) 將long轉換為固定格式時間字串