Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 代碼小金庫:常用工具方法大合集

編輯:關於 Android 程式設計

Android 代碼小金庫

我一直覺得,Android 開發應該有一個自己的「代碼小金庫」,用來收集那些常用的代碼塊,需要的時候直接拿來即用,開發速度簡直能快一倍。下面就把它分享出來。

1. Activity 相關 → ActivityUtils.java

 isActivityExists : 判斷是否存在 Activity launchActivity : 打開 Activity

2. App 相關 → AppUtils.java

 isInstallApp : 判斷 App 是否安裝 installApp : 安裝 App(支持6.0) installAppSilent : 靜默安裝 App uninstallApp : 卸載 App uninstallAppSilent : 靜默卸載 App isAppRoot : 判斷 App 是否有 root 權限 launchApp : 打開 App closeApp : 關閉 App getAppPackageName : 獲取 App 包名 getAppDetailsSettings : 獲取 App 具體設置 getAppName : 獲取 App 名稱 getAppIcon : 獲取 App 圖標 getAppPath : 獲取 App 路徑 getAppVersionName : 獲取 App 版本號 getAppVersionCode : 獲取 App 版本碼 isSystemApp : 判斷 App 是否是系統應用 isAppDebug : 判斷 App 是否是 Debug 版本 getAppSignature : 獲取 App 簽名 getAppSignatureSHA1 : 獲取應用簽名的 SHA1 值 isAppForeground : 判斷 App 是否處於前台 getForegroundApp : 獲取前台應用包名 getAppInfo : 獲取 App 信息 getAppsInfo : 獲取所有已安裝 App 信息 cleanAppData : 清除 App 所有數據

3. 欄相關 → BarUtils.java

 setTransparentStatusBar : 設置透明狀態欄(api 大於 19 方可使用) hideStatusBar : 隱藏狀態欄 getStatusBarHeight : 獲取狀態欄高度 isStatusBarExists : 判斷狀態欄是否存在 getActionBarHeight : 獲取 ActionBar 高度 showNotificationBar : 顯示通知欄 hideNotificationBar : 隱藏通知欄

4. 清除相關 → CleanUtils.java

 cleanInternalCache : 清除內部緩存 cleanInternalFiles : 清除內部文件 cleanInternalDbs : 清除內部數據庫 cleanInternalDbByName : 根據名稱清除數據庫 cleanInternalSP : 清除內部 SP cleanExternalCache : 清除外部緩存 cleanCustomCache : 清除自定義目錄下的文件

5. 剪貼板相關 → ClipboardUtils.java

 copyText : 復制文本到剪貼板 getText : 獲取剪貼板的文本 copyUri : 復制 uri 到剪貼板 getUri : 獲取剪貼板的 uri copyIntent : 復制意圖到剪貼板 getIntent : 獲取剪貼板的意圖

6. 關閉相關 → CloseUtils.java

 closeIO : 關閉 IO closeIOQuietly : 安靜關閉 IO

7. 常量相關 → ConstUtils.java

 MemoryConst : 存儲相關常量 TimeConst : 時間相關常量 RegexConst : 正則相關常量

8. 轉換相關 → ConvertUtils.java

 bytes2HexString, hexString2Bytes : byteArr 與 hexString 互轉 chars2Bytes, bytes2Chars : charArr 與 byteArr 互轉 byte2Size, size2Byte : 字節數與 unit 為單位的 size 互轉 byte2FitSize : 字節數轉合適大小 bytes2Bits, bits2Bytes : bytes 與 bits 互轉 input2OutputStream, output2InputStream : inputStream 與 outputStream 互轉 inputStream2Bytes, bytes2InputStream : inputStream 與 byteArr 互轉 outputStream2Bytes, bytes2OutputStream : outputStream 與 byteArr 互轉 inputStream2String, string2InputStream : inputStream 與 string 按編碼互轉 outputStream2String, string2OutputStream : outputStream 與 string 按編碼互轉 bitmap2Bytes, bytes2Bitmap : bitmap 與 byteArr 互轉 drawable2Bitmap, bitmap2Drawable : drawable 與 bitmap 互轉 drawable2Bytes, bytes2Drawable : drawable 與 byteArr 互轉 view2Bitmap : view 轉 Bitmap dp2px, px2dp : dp 與 px 互轉 sp2px, px2sp : sp 與 px 互轉

9. 崩潰相關 → CrashUtils.java

 getInstance : 獲取單例 init : 初始化

10. 設備相關 → DeviceUtils.java

 isDeviceRoot : 判斷設備是否 root getSDKVersion : 獲取設備系統版本號 getAndroidID : 獲取設備 AndroidID getMacAddress : 獲取設備 MAC 地址 getManufacturer : 獲取設備廠商 getModel : 獲取設備型號 shutdown : 關機 reboot : 重啟

11. 判空相關 → EmptyUtils.java

 isEmpty : 判斷對象是否為空 isNotEmpty : 判斷對象是否非空

12. 編碼解碼相關 → EncodeUtils.java

 urlEncode : URL 編碼 urlDecode : URL 解碼 base64Encode, base64Encode2String : Base64 編碼 base64Decode : Base64 解碼 base64UrlSafeEncode : Base64URL 安全編碼 htmlEncode : Html 編碼 htmlDecode : Html 解碼

13. 加密解密相關 → EncryptUtils.java

 encryptMD2, encryptMD2ToString : MD2 加密 encryptMD5, encryptMD5ToString : MD5 加密 encryptMD5File, encryptMD5File2String : MD5 加密文件 encryptSHA1, encryptSHA1ToString : SHA1 加密 encryptSHA224~SHA512 : SHA 系列加密 encryptHmacMD5~HmacSHA512 : Hmac 系列加密 encryptDES, decryptDES : DES 加解密 encrypt3DES, decrypt3DES : 3DES 加解密 encryptAES, decryptAES : AES 加解密

14. 文件相關 → FileUtils.java

 getFileByPath : 根據文件路徑獲取文件 isFileExists : 判斷文件是否存在 rename : 重命名文件 isDir / isFile : 判斷是目錄還是文件 createOrExistsDir / File : 判斷並創建 copyDir / copyFile : 復制 moveDir / moveFile : 移動 deleteDir / deleteFile : 刪除 listFilesInDir : 獲取目錄下所有文件 searchFileInDir : 搜索指定文件名 writeFileFromIS / writeFileFromString : 寫入文件 getFileCharsetSimple : 獲取文件編碼 getFileLines : 獲取文件行數 readFile2List / readFile2SB : 按行讀取 getFileSize : 獲取文件大小 getFileMD5 : 獲取文件 MD5 getDirName / getFileName / getFileExtension : 解析路徑

15. 圖片相關 → ImageUtils.java

 bitmap2Bytes, bytes2Bitmap : bitmap 與 byteArr 互轉 drawable2Bitmap, bitmap2Drawable : drawable 與 bitmap 互轉 getBitmap : 獲取 bitmap scale : 縮放圖片 clip : 裁剪圖片 skew : 傾斜圖片 rotate : 旋轉圖片 toRound / toRoundCorner : 轉圓形/圓角 fastBlur / renderScriptBlur / stackBlur : 模糊 addFrame / addReflection : 邊框/倒影 addTextWatermark / addImageWatermark : 水印 toAlpha / toGray : alpha/灰度 save : 保存圖片 compressByScale / Quality / SampleSize : 壓縮

16. 意圖相關 → IntentUtils.java

 getInstallAppIntent : 安裝 App 意圖 getUninstallAppIntent : 卸載 App 意圖 getLaunchAppIntent : 打開 App 意圖 getAppDetailsSettingsIntent : App 設置意圖 getShareTextIntent / getShareImageIntent : 分享意圖 getComponentIntent : 其他組件意圖 getShutdownIntent : 關機意圖 getCaptureIntent : 拍照意圖

17. 鍵盤相關 → KeyboardUtils.java

 hideSoftInput : 隱藏軟鍵盤 clickBlankArea2HideSoftInput : 點擊空白處隱藏 showSoftInput : 顯示軟鍵盤 toggleSoftInput : 切換鍵盤顯示

18. 日志相關 → LogUtils.java

 init : 初始化 getBuilder : 獲取建造者 v / d / i / w / e : 各級別日志

19. 網絡相關 → NetworkUtils.java

 openWirelessSettings : 打開網絡設置 isConnected : 網絡是否連接 isAvailableByPing : 網絡是否可用 getDataEnabled / setDataEnabled : 移動數據開關 is4G : 是否 4G getWifiEnabled / setWifiEnabled : WiFi 開關 isWifiConnected / isWifiAvailable : WiFi 狀態 getNetworkOperatorName : 運營商名稱 getNetworkType : 網絡類型 getIPAddress / getDomainAddress : IP/域名解析

20. 手機相關 → PhoneUtils.java

 isPhone : 是否手機 getIMEI / getIMSI : 獲取 IMEI/IMSI getPhoneType : 移動終端類型 getSimOperatorName : Sim 卡運營商 getPhoneStatus : 手機狀態信息 dial : 跳至撥號界面 call : 撥打電話 sendSms / sendSmsSilent : 發送短信 getAllContactInfo : 手機聯系人 getAllSMS : 手機短信

21. 進程相關 → ProcessUtils.java

 getForegroundProcessName : 前台線程包名 cleanAllBackgroundProcesses : 清理後台進程

22. 正則相關 → RegexUtils.java

 isMobileSimple / isMobileExact : 驗證手機號 isTel : 驗證電話號碼 isIDCard15 / isIDCard18 : 驗證身份證 isEmail : 驗證郵箱 isURL : 驗證 URL isChz : 驗證漢字 isUsername : 驗證用戶名 isDate : 驗證日期 isIP : 驗證 IP isMatch : 是否匹配

23. 屏幕相關 → ScreenUtils.java

 getDeviceWidth / getDeviceHeight : 屏幕分辨率 setTransparentStatusBar : 透明狀態欄 hideStatusBar : 隱藏狀態欄 getStatusBarHeight : 狀態欄高度 getActionBarHeight : ActionBar 高度 showNotificationBar / hideNotificationBar : 通知欄 setLandscape : 設置橫屏 snapShotWithStatusBar / snapShotWithoutStatusBar : 截屏 isScreenLock : 是否鎖屏

24. SD 卡相關 → SDCardUtils.java

 isSDCardEnable : SD 卡是否可用 getSDCardPath : SD 卡路徑 getDataPath : Data 路徑 getFreeSpace : 剩余空間 getSDCardInfo : SD 卡信息

25. 服務相關 → ServiceUtils.java

 isServiceRunning : 服務是否運行 stopService : 停止服務

26. Shell 相關 → ShellUtils.java

 execCmd : 是否在 root 下執行命令

27. 尺寸相關 → SizeUtils.java

 dp2px, px2dp : dp 與 px 轉換 sp2px, px2sp : sp 與 px 轉換 applyDimension : 單位轉換 forceGetViewSize : onCreate 強行獲取尺寸 measureView : ListView 中提前測量

28. Snackbar 相關 → SnackbarUtils.java

 showShortSnackbar / showLongSnackbar / showIndefiniteSnackbar : 顯示 snackbar addView : 添加布局 dismissSnackbar : 取消顯示

29. SP 相關 → SPUtils.java

 putString / getString, putInt / getInt, putLong / getLong, putFloat / getFloat, putBoolean / getBoolean : 讀寫各類型 getAll : 獲取所有 remove : 移除 key contains : 是否存在 clear : 清除

30. 字符串相關 → StringUtils.java

 isEmpty / isSpace : 判空 equals : 是否相等 null2Length0 : null 轉空串 length : 長度 upperFirstLetter / lowerFirstLetter : 首字母大小寫 reverse : 反轉 toDBC / toSBC : 半角/全角 getPYFirstLetter : 漢字首字母 cn2PY : 中文轉拼音

31. 線程池相關 → ThreadPoolUtils.java

 execute : 執行命令 shutDown / shutDownNow : 關閉線程池 isShutDown / isTerminated : 狀態查詢 submit : 提交任務 invokeAll / invokeAny : 批量執行 schedule : 延遲執行 scheduleWithFixedRate / FixedDelay : 週期執行

32. 時間相關 → TimeUtils.java

 milliseconds2String / string2Milliseconds : 時間戳與字符串互轉 string2Date / date2String : 字符串與 Date 互轉 date2Milliseconds / milliseconds2Date : Date 與時間戳互轉 getIntervalTime : 時間差 getCurTimeMills / String / Date : 當前時間 isLeapYear : 是否閏年 getWeek / getWeekOfMonth / getWeekOfYear : 獲取星期/周數

33. 吐司相關 → ToastUtils.java

 init : 初始化 showShortToastSafe / showLongToastSafe : 安全顯示 showShortToast / showLongToast : 顯示吐司 cancelToast : 取消顯示

34. 壓縮相關 → ZipUtils.java

 zipFiles / zipFile : 壓縮 unzipFiles / unzipFile : 解壓 unzipFileByKeyword : 按關鍵字解壓 getFilesPath : 壓縮內文件路徑 getComments : 注釋 getEntries : 文件對象

這個「代碼小金庫」基本覆蓋了 Android 開發中大部分常用場景。這些都是網上搜集整理、公開共享出來的,希望能給大家的開發帶來便利。當然,最好的方式還是親自動手寫一寫、感受一下,收穫會更大。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved