Android開發常用工具類

來源:互聯網
上載者:User

標籤:

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

       目前包括  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(HttpRequest request)         static HttpResponse httpGet(java.lang.String httpUrl)         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。         源碼可見HttpUtils.java,更多方法及更詳細參數介紹可見HttpUtils Api Guide。

 

       2、DownloadManagerPro        Android系統下載管理DownloadManager增強方法,可用於包括擷取下載相關資訊,如:        getStatusById(long) 得到下載狀態        getDownloadBytes(long) 得到下載進度資訊        getBytesAndStatus(long) 得到下載進度資訊和狀態        getFileName(long) 得到下載檔案路徑        getUri(long) 得到下載uri        getReason(long) 得到下載失敗或暫停原因        getPausedReason(long) 得到下載暫停原因        getErrorCode(long) 得到下載錯誤碼        源碼可見DownloadManagerPro.java,更多方法及更詳細參數介紹可見DownloadManagerPro Api Guide。

 

       3、Safe.ijiami

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

       

 

        4、ShellUtils        Android Shell工具類,可用於檢查系統root許可權,並在shell或root使用者下執行shell命令。如:checkRootPermission() 檢查root許可權execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) shell環境執行命令,第二個參數表示是否root許可權執行execCommand(String command, boolean isRoot) shell環境執行命令        源碼可見ShellUtils.java,更多方法及更詳細參數介紹可見ShellUtils Api Guide。

 

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

        如:install(Context, String) 安裝應用,如果是系統應用或已經root,則靜默安裝,否則一般安裝uninstall(Context, String) 卸載應用,如果是系統應用或已經root,則靜默卸載,否則一般卸載isSystemApplication(Context, String) 判斷應用是否為系統應用         源碼可見PackageUtils.java,更多方法及更詳細參數介紹可見ShellUtils Api Guide。

 

         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         源碼可見PreferencesUtils.java,更多方法及更詳細參數介紹可見PreferencesUtils Api Guide。

 

        7、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的值         源碼可見JSONUtils.java,更多方法及更詳細參數介紹可見JSONUtils Api Guide。

 

        8、FileUtils         檔案工具類,可用於讀寫檔案及對檔案進行操作。如:         readFile(String filePath) 讀檔案         writeFile(String filePath, String content, boolean append) 寫檔案         getFileSize(String path) 得到檔案大小         deleteFile(String path) 刪除檔案         源碼可見FileUtils.java,更多方法及更詳細參數介紹可見FileUtils Api Guide。

 

         9、ResourceUtils         Android Resource工具類,可用於從android資來源目錄的raw和assets目錄讀取內容,如:         geFileFromAssets(Context context, String fileName) 得到assets目錄下某個檔案內容         geFileFromRaw(Context context, int resId) 得到raw目錄下某個檔案內容         源碼可見ResourceUtils.java,更多方法及更詳細參數介紹可見ResourceUtils Api Guide。

 

        10、StringUtils         String工具類,可用於常見字串操作,如:         isEmpty(String str) 判斷字串是否為空白或長度為0         isBlank(String str) 判斷字串是否為空白或長度為0 或由空格組成         utf8Encode(String str) 以utf-8格式編碼         capitalizeFirstLetter(String str) 首字母大寫         源碼可見StringUtils.java,更多方法及更詳細參數介紹可見StringUtils Api Guide。

 

      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<K, V> map, Parcel out, int flags) 向parcel中寫入map類型資料         源碼可見ParcelUtils.java,更多方法及更詳細參數介紹可見ParcelUtils Api Guide。

 

       12、RandomUtils         隨機數工具類,可用於擷取固定大小固定字元內的隨機數,如:         getRandom(char[] sourceChar, int length) 產生隨機字串,所有字元均在某個字串內         getRandomNumbers(int length) 產生隨機數字         源碼可見RandomUtils.java,更多方法及更詳細參數介紹可見RandomUtils Api Guide。

 

       13、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表示是否迴圈         源碼可見ArrayUtils.java,更多方法及更詳細參數介紹可見ArrayUtils Api Guide。

 

       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) 縮放圖片         源碼可見ImageUtils.java,更多方法及更詳細參數介紹可見ImageUtils Api Guide。

 

         15、ListUtils          List工具類,可用於List常用操作,如:         isEmpty(List<V> sourceList) 判斷List是否為空白或長度為0         join(List<String> list, String separator) List轉換為字串,並以固定分隔字元分割         addDistinctEntry(List<V> sourceList, V entry) 向list中添加不重複元素        源碼可見ListUtils.java,更多方法及更詳細參數介紹可見ListUtils Api Guide。

 

      16、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格式         源碼可見MapUtils.java,更多方法及更詳細參數介紹可見MapUtils Api Guide。

 

        17、ObjectUtils         Object工具類,可用於Object常用操作,如:         isEquals(Object actual, Object expected) 比較兩個對象是否相等         compare(V v1, V v2) 比較兩個對象大小         transformIntArray(int[] source)  Integer 數群組轉換為int數組         源碼可見ObjectUtils.java,更多方法及更詳細參數介紹可見ObjectUtils Api Guide。

 

       18、SerializeUtils         序列化工具類,可用於序列化對象到檔案或從檔案還原序列化對象,如:        deserialization(String filePath) 從檔案還原序列化對象        serialization(String filePath, Object obj) 序列化對象到檔案        源碼可見SerializeUtils.java,更多方法及更詳細參數介紹可見SerializeUtils Api Guide。

 

      19、SystemUtils        系統資訊工具類,可用於得到線程池合適的大小,目前功能薄弱,後面會進行增強。如:         getDefaultThreadPoolSize() 得到跟系統配置相符的線程池大小         源碼可見SystemUtils.java,更多方法及更詳細參數介紹可見SystemUtils Api Guide。

 

        20、TimeUtils         時間工具類,可用於時間相關操作,如:         getCurrentTimeInLong() 得到目前時間        getTime(long timeInMillis, SimpleDateFormat dateFormat) 將long轉換為固定格式時間字串        源碼可見TimeUtils.java,更多方法及更詳細參數介紹可見TimeUtils Api Guide。

Android開發常用工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.