1. 程式人生 > >Android開發人員不得不收集的程式碼(blankj:utilcode)

Android開發人員不得不收集的程式碼(blankj:utilcode)

Android開發人員不得不收集的程式碼(持續更新中)

為方便查詢,已進行大致歸類,其目錄如下所示:

  • isActivityExists    : 判斷是否存在Activity
    launchActivity      : 開啟Activity
    getLauncherActivity : 獲取入口activity
  • 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所有資料
  • setTransparentStatusBar : 設定透明狀態列(api大於19方可使用)
    hideStatusBar           : 隱藏狀態列
    getStatusBarHeight      : 獲取狀態列高度
    isStatusBarExists       : 判斷狀態列是否存在
    getActionBarHeight      : 獲取ActionBar高度
    showNotificationBar     : 顯示通知欄
    hideNotificationBar     : 隱藏通知欄
  • cleanInternalCache    : 清除內部快取
    cleanInternalFiles    : 清除內部檔案
    cleanInternalDbs      : 清除內部資料庫
    cleanInternalDbByName : 根據名稱清除資料庫
    cleanInternalSP       : 清除內部SP
    cleanExternalCache    : 清除外部快取
    cleanCustomCache      : 清除自定義目錄下的檔案
  • copyText   : 複製文字到剪貼簿
    getText    : 獲取剪貼簿的文字
    copyUri    : 複製uri到剪貼簿
    getUri     : 獲取剪貼簿的uri
    copyIntent : 複製意圖到剪貼簿
    getIntent  : 獲取剪貼簿的意圖
  • closeIO        : 關閉IO
    closeIOQuietly : 安靜關閉IO
  • MemoryConst : 儲存相關常量
    TimeConst   : 時間相關常量
    RegexConst  : 正則相關常量
  • 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互轉
  • getInstance : 獲取單例
    init        : 初始化
  • isDeviceRoot      : 判斷裝置是否root
    getSDKVersion     : 獲取裝置系統版本號
    getAndroidID      : 獲取裝置AndroidID
    getMacAddress     : 獲取裝置MAC地址
    getManufacturer   : 獲取裝置廠商
    getModel          : 獲取裝置型號
    shutdown          : 關機
    reboot            : 重啟
    reboot2Recovery   : 重啟到recovery
    reboot2Bootloader : 重啟到bootloader
  • isEmpty    : 判斷物件是否為空
    isNotEmpty : 判斷物件是否非空
  • urlEncode                         : URL編碼
    urlDecode                         : URL解碼
    base64Encode, base64Encode2String : Base64編碼
    base64Decode                      : Base64解碼
    base64UrlSafeEncode               : Base64URL安全編碼
    htmlEncode                        : Html編碼
    htmlDecode                        : Html解碼
  • encryptMD2, encryptMD2ToString                         : MD2加密
    encryptMD5, encryptMD5ToString                         : MD5加密
    encryptMD5File, encryptMD5File2String                  : MD5加密檔案
    encryptSHA1, encryptSHA1ToString                       : SHA1加密
    encryptSHA224, encryptSHA224ToString                   : SHA224加密
    encryptSHA256, encryptSHA256ToString                   : SHA256加密
    encryptSHA384, encryptSHA384ToString                   : SHA384加密
    encryptSHA512, encryptSHA512ToString                   : SHA512加密
    encryptHmacMD5, encryptHmacMD5ToString                 : HmacMD5加密
    encryptHmacSHA1, encryptHmacSHA1ToString               : HmacSHA1加密
    encryptHmacSHA224, encryptHmacSHA224ToString           : HmacSHA224加密
    encryptHmacSHA256, encryptHmacSHA256ToString           : HmacSHA256加密
    encryptHmacSHA384, encryptHmacSHA384ToString           : HmacSHA384加密
    encryptHmacSHA512, encryptHmacSHA512ToString           : HmacSHA512加密
    encryptDES, encryptDES2HexString, encryptDES2Base64    : DES加密
    decryptDES, decryptHexStringDES, decryptBase64DES      : DES解密
    encrypt3DES, encrypt3DES2HexString, encrypt3DES2Base64 : 3DES加密
    decrypt3DES, decryptHexString3DES, decryptBase64_3DES  : 3DES解密
    encryptAES, encryptAES2HexString, encryptAES2Base64    : AES加密
    decryptAES, decryptHexStringAES, decryptBase64AES      : AES解密
  • getFileByPath                  : 根據檔案路徑獲取檔案
    isFileExists                   : 判斷檔案是否存在
    rename                         : 重新命名檔案
    isDir                          : 判斷是否是目錄
    isFile                         : 判斷是否是檔案
    createOrExistsDir              : 判斷目錄是否存在,不存在則判斷是否建立成功
    createOrExistsFile             : 判斷檔案是否存在,不存在則判斷是否建立成功
    createFileByDeleteOldFile      : 判斷檔案是否存在,存在則在建立之前刪除
    copyDir                        : 複製目錄
    copyFile                       : 複製檔案
    moveDir                        : 移動目錄
    moveFile                       : 移動檔案
    deleteDir                      : 刪除目錄
    deleteFile                     : 刪除檔案
    listFilesInDir                 : 獲取目錄下所有檔案
    listFilesInDir                 : 獲取目錄下所有檔案包括子目錄
    listFilesInDirWithFilter       : 獲取目錄下所有後綴名為suffix的檔案
    listFilesInDirWithFilter       : 獲取目錄下所有後綴名為suffix的檔案包括子目錄
    listFilesInDirWithFilter       : 獲取目錄下所有符合filter的檔案
    listFilesInDirWithFilter       : 獲取目錄下所有符合filter的檔案包括子目錄
    searchFileInDir                : 獲取目錄下指定檔名的檔案包括子目錄
    writeFileFromIS                : 將輸入流寫入檔案
    writeFileFromString            : 將字串寫入檔案
    getFileCharsetSimple           : 簡單獲取檔案編碼格式
    getFileLines                   : 獲取檔案行數
    readFile2List                  : 指定編碼按行讀取檔案到List
    readFile2SB                    : 指定編碼按行讀取檔案到StringBuilder中
    getFileSize                    : 獲取檔案大小
    getFileMD5, getFileMD5ToString : 獲取檔案的MD5校驗碼
    getDirName                     : 根據全路徑獲取最長目錄
    getFileName                    : 根據全路徑獲取檔名
    getFileNameNoExtension         : 根據全路徑獲取檔名不帶拓展名
    getFileExtension               : 根據全路徑獲取檔案拓展名
  • HandlerHolder : 使用必讀
  • bitmap2Bytes, bytes2Bitmap       : bitmap與byteArr互轉
    drawable2Bitmap, bitmap2Drawable : drawable與bitmap互轉
    drawable2Bytes, bytes2Drawable   : drawable與byteArr互轉
    getBitmap                        : 獲取bitmap
    scale                            : 縮放圖片
    clip                             : 裁剪圖片
    skew                             : 傾斜圖片
    rotate                           : 旋轉圖片
    getRotateDegree                  : 獲取圖片旋轉角度
    toRound                          : 轉為圓形圖片
    toRoundCorner                    : 轉為圓角圖片
    fastBlur                         : 快速模糊
    renderScriptBlur                 : renderScript模糊圖片
    stackBlur                        : stack模糊圖片
    addFrame                         : 新增顏色邊框
    addReflection                    : 新增倒影
    addTextWatermark                 : 新增文字水印
    addImageWatermark                : 新增圖片水印
    toAlpha                          : 轉為alpha點陣圖
    toGray                           : 轉為灰度圖片
    save                             : 儲存圖片
    isImage                          : 根據檔名判斷檔案是否為圖片
    getImageType                     : 獲取圖片型別
    compressByScale                  : 按縮放壓縮
    compressByQuality                : 按質量壓縮
    compressBySampleSize             : 按取樣大小壓縮
  • getInstallAppIntent         : 獲取安裝App(支援6.0)的意圖
    getUninstallAppIntent       : 獲取解除安裝App的意圖
    getLaunchAppIntent          : 獲取開啟App的意圖
    getAppDetailsSettingsIntent : 獲取App具體設定的意圖
    getShareTextIntent          : 獲取分享文字的意圖
    getShareImageIntent         : 獲取分享圖片的意圖
    getComponentIntent          : 獲取其他應用元件的意圖
    getShutdownIntent           : 獲取關機的意圖
    getCaptureIntent            : 獲取拍照的意圖
  • hideSoftInput                : 動態隱藏軟鍵盤
    clickBlankArea2HideSoftInput : 點選螢幕空白區域隱藏軟鍵盤
    showSoftInput                : 動態顯示軟鍵盤
    toggleSoftInput              : 切換鍵盤顯示與否狀態
  • isGpsEnabled      : 判斷Gps是否可用
    isLocationEnabled : 判斷定位是否可用
    openGpsSettings   : 開啟Gps設定介面
    register          : 註冊
    unregister        : 登出
    getAddress        : 根據經緯度獲取地理位置
    getCountryName    : 根據經緯度獲取所在國家
    getLocality       : 根據經緯度獲取所在地
    getStreet         : 根據經緯度獲取所在街道
  • init       : 初始化函式
    getBuilder : 獲取LogUtils建造者
    v          : Verbose日誌
    d          : Debug日誌
    i          : Info日誌
    w          : Warn日誌
    e          : Error日誌
  • openWirelessSettings   : 開啟網路設定介面
    isConnected            : 判斷網路是否連線
    isAvailableByPing      : 判斷網路是否可用
    getDataEnabled         : 判斷移動資料是否開啟
    setDataEnabled         : 開啟或關閉移動資料
    is4G                   : 判斷網路是否是4G
    getWifiEnabled         : 判斷wifi是否開啟
    setWifiEnabled         : 開啟或關閉wifi
    isWifiConnected        : 判斷wifi是否連線狀態
    isWifiAvailable        : 判斷wifi資料是否可用
    getNetworkOperatorName : 獲取行動網路運營商名稱
    getNetworkType         : 獲取當前網路型別
    getIPAddress           : 獲取IP地址
    getDomainAddress       : 獲取域名ip地址
  • isPhone                                 : 判斷裝置是否是手機
    getIMEI                                 : 獲取IMEI碼
    getIMSI                                 : 獲取IMSI碼
    getPhoneType                            : 獲取移動終端型別
    isSimCardReady                          : 判斷sim卡是否準備好
    getSimOperatorName, getSimOperatorByMnc : 獲取Sim卡運營商名稱
    getPhoneStatus                          : 獲取手機狀態資訊
    dial                                    : 跳至撥號介面
    call                                    : 撥打phoneNumber
    sendSms                                 : 跳至傳送簡訊介面
    sendSmsSilent                           : 傳送簡訊
    getAllContactInfo                       : 獲取手機聯絡人
    getContactNum                           : 開啟手機聯絡人介面點選聯絡人後便獲取該號碼
    getAllSMS                               : 獲取手機簡訊並儲存到xml
  • getPinyinFirstLetter : 獲取第一個漢字首字母
    ccs2Pinyin           : 漢字轉拼音
  • getForegroundProcessName                              : 獲取前臺執行緒包名
    cleanAllBackgroundProcesses, cleanBackgroundProcesses : 清理後臺服務程序
  • isMobileSimple  : 驗證手機號(簡單)
    isMobileExact   : 驗證手機號(精確)
    isTel           : 驗證電話號碼
    isIDCard15      : 驗證身份證號碼15位
    isIDCard18      : 驗證身份證號碼18位
    isEmail         : 驗證郵箱
    isURL           : 驗證URL
    isZh            : 驗證漢字
    isUsername      : 驗證使用者名稱
    isDate          : 驗證yyyy-MM-dd格式的日期校驗,已考慮平閏年
    isIP            : 驗證IP地址
    isMatch         : 判斷是否匹配正則
    getMatches      : 獲取正則匹配的部分
    getSplits       : 獲取正則匹配分組
    getReplaceFirst : 替換正則匹配的第一部分
    getReplaceAll   : 替換所有正則匹配的部分
  • getScreenWidth          : 獲取螢幕的寬度(單位:px)
    getScreenHeight         : 獲取螢幕的高度(單位:px)
    setLandscape            : 設定螢幕為橫屏
    setPortrait             : 設定螢幕為豎屏
    isLandscape             : 判斷是否橫屏
    isPortrait              : 判斷是否豎屏
    getScreenRotation       : 獲取螢幕旋轉角度
    captureWithStatusBar    : 獲取當前螢幕截圖,包含狀態列
    captureWithoutStatusBar : 獲取當前螢幕截圖,不包含狀態列
    isScreenLock            : 判斷是否鎖屏
  • isSDCardEnable : 判斷SD卡是否可用
    getSDCardPath  : 獲取SD卡路徑
    getDataPath    : 獲取SD卡Data路徑
    getFreeSpace   : 計算SD卡的剩餘空間
    getSDCardInfo  : 獲取SD卡資訊
  • isServiceRunning : 判斷服務是否執行
    stopService      : 停止服務
  • execCmd : 是否是在root下執行命令
  • dp2px, px2dp      : dppx轉換
    sp2px, px2sp      : sppx轉換
    applyDimension    : 各種單位轉換
    forceGetViewSize  : 在onCreate中獲取檢視的尺寸
    measureView       : 測量檢視尺寸
    getMeasuredWidth  : 獲取測量檢視寬度
    getMeasuredHeight : 獲取測量檢視高度
  • showShortSnackbar      : 顯示短時snackbar
    showLongSnackbar       : 顯示長時snackbar
    showIndefiniteSnackbar : 顯示自定義時長snackbar
    addView                : 為SnackBar添加布局
    dismissSnackbar        : 取消snackbar顯示
  • SPUtils    : SPUtils建構函式
    putString  : SP中寫入String型別value
    getString  : SP中讀取String
    putInt     : SP中寫入int型別value
    getInt     : SP中讀取int
    putLong    : SP中寫入long型別value
    getLong    : SP中讀取long
    putFloat   : SP中寫入float型別value
    getFloat   : SP中讀取float
    putBoolean : SP中寫入boolean型別value
    getBoolean : SP中讀取boolean
    getAll     : SP中獲取所有鍵值對
    remove     : SP中移除該key
    contains   : SP中是否存在該key
    clear      : SP中清除所有資料
  • isEmpty          : 判斷字串是否為null或長度為0
    isSpace          : 判斷字串是否為null或全為空格
    equals           : 判斷兩字串是否相等
    equalsIgnoreCase : 判斷兩字串忽略大小寫是否相等
    null2Length0     : null轉為長度為0的字串
    length           : 返回字串長度
    upperFirstLetter : 首字母大寫
    lowerFirstLetter : 首字母小寫
    reverse          : 反轉字串
    toDBC            : 轉化為半形字元
    toSBC            : 轉化為全形字元
  • ThreadPoolUtils        : ThreadPoolUtils建構函式
    execute                : 在未來某個時間執行給定的命令
    execute                : 在未來某個時間執行給定的命令連結串列
    shutDown               : 待以前提交的任務執行完畢後關閉執行緒池
    shutDownNow            : 試圖停止所有正在執行的活動任務
    isShutDown             : 判斷執行緒池是否已關閉
    isTerminated           : 關閉執行緒池後判斷所有任務是否都已完成
    awaitTermination       : 請求關閉、發生超時或者當前執行緒中斷
    submit                 : 提交一個Callable任務用於執行
    submit                 : 提交一個Runnable任務用於執行
    invokeAll, invokeAny   : 執行給定的任務
    schedule               : 延遲執行Runnable命令
    schedule               : 延遲執行Callable命令
    scheduleWithFixedRate  : 延遲並迴圈執行命令
    scheduleWithFixedDelay : 延遲並以固定休息時間迴圈執行命令
  • milliseconds2String   : 將時間戳轉為時間字串
    string2Milliseconds   : 將時間字串轉為時間戳
    string2Date           : 將時間字串轉為Date型別
    date2String           : 將Date型別轉為時間字串
    date2Milliseconds     : 將Date型別轉為時間戳
    milliseconds2Date     : 將時間戳轉為Date型別
    milliseconds2Unit     : 毫秒時間戳單位轉換(單位:unit)
    getIntervalTime       : 獲取兩個時間差(單位:unit)
    getCurTimeMills       : 獲取當前時間戳
    getCurTimeString      : 獲取當前時間字串
    getCurTimeDate        : 獲取當前Date
    getIntervalByNow      : 獲取與當前時間的差(單位:unit)
    isLeapYear            : 判斷閏年
    getWeek, getWeekIndex : 獲取星期
    getWeekOfMonth        : 獲取月份中的第幾周
    getWeekOfYear         : 獲取年份中的第幾周
  • init               : 吐司初始化
    showShortToastSafe : 安全地顯示短時吐司
    showLongToastSafe  : 安全地顯示長時吐司
    showShortToast     : 顯示短時吐司
    showLongToast      : 顯示長時吐司
    cancelToast        : 取消吐司顯示
  • zipFiles           : 批量壓縮檔案
    zipFile            : 壓縮檔案
    unzipFiles         : 批量解壓檔案
    unzipFile          : 解壓檔案
    unzipFileByKeyword : 解壓帶有關鍵字的檔案
    getFilesPath       : 獲取壓縮檔案中的檔案路徑連結串列
    getComments        : 獲取壓縮檔案中的註釋連結串列
    getEntries         : 獲取壓縮檔案中的檔案物件

關於

  • 做這份整理是想把它作為Android開發的小字典,當遇到一些瑣碎問題時,不用再面向百度或者谷歌查詢API的使用,費時費力,這裡有的話,大家儘管擼走;同時也希望它能逐日壯大起來,期待大家的Star和完善,當然我也會一直更新發布版本和日誌,為了方便大家匯入,現已上傳jcenter;其中很多程式碼也是匯四方之精華,謝謝前輩們的提供,當然最終還是要通過單元測試的,如有錯誤,請及時告之。
  • QQ群提供討論,1群:74721490(已滿)2群:25206533,至於驗證問題對大家來說肯定都是小case。
  • 我的微博,求個關注哈。

Download

Gradle:

compile 'com.blankj:utilcode:1.3.4'

Proguard

-keep class com.blankj.utilcode.** { *; }
-keepclassmembers class com.blankj.utilcode.** { *; }
-dontwarn com.blankj.utilcode.**

License

Copyright 2016 Blankj

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


文/Blankj(簡書作者)
原文連結:http://www.jianshu.com/p/72494773aace
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。