Util 模块是通用工具类集合模块,提供了丰富的 Android 开发常用工具类和扩展函数。该模块涵盖了系统工具、UI 工具、数据处理、文件操作、设备信息、性能优化等多个方面的实用功能。
// 获取设备信息
DeviceUtil.getDeviceInfo()
DeviceUtil.isTablet(context)
DeviceUtil.getScreenDensity(context)
// 应用工具
AppUtil.getVersionName(context)
AppUtil.getVersionCode(context)
AppUtil.isAppInstalled(context, packageName)
// 系统工具
SystemUtils.isMainThread()
SystemUtils.getProcessName(context)
// 单位转换
UnitUtil.dp2px(context, dpValue)
UnitUtil.px2dp(context, pxValue)
UnitUtil.sp2px(context, spValue)
// 视图工具
ViewUtil.setVisible(view, isVisible)
ViewUtil.measureView(view)
ViewUtil.getViewLocation(view)
// 截图工具
ViewShotUtil.takeScreenshot(view)
ViewShotUtil.saveViewAsImage(view, file)
// 文件操作
FileUtil.createFile(path)
FileUtil.deleteFile(file)
FileUtil.copyFile(source, dest)
FileUtil.getFileSize(file)
// Asset 资源
AssetUtil.readAssetFile(context, fileName)
AssetUtil.copyAssetFile(context, assetName, destPath)
// 压缩工具
CompressUtil.compressImage(imagePath, quality)
GzipUtils.compress(data)
GzipUtils.decompress(compressedData)
// 网络状态
NetworkUtil.isNetworkAvailable(context)
NetworkUtil.getNetworkType(context)
NetworkUtil.isWifiConnected(context)
// 电话号码
PhoneUtil.isValidPhoneNumber(phoneNumber)
PhoneUtil.formatPhoneNumber(phoneNumber, countryCode)
// 哈希工具
HashUtil.md5(input)
HashUtil.sha1(input)
HashUtil.sha256(input)
// 十六进制
HexUtil.bytesToHex(bytes)
HexUtil.hexToBytes(hex)
// 字符串处理
StringUtil.isEmpty(str)
StringUtil.isNotEmpty(str)
StringUtil.formatString(template, args)
// 字符串扩展
"hello".isNotNullOrEmpty()
"123".toIntOrDefault(0)
// 列表扩展
list.isNotNullOrEmpty()
list.safeGet(index)
// 颜色扩展
Color.parseColor("#FF0000").toHexString()
// Intent 扩展
intent.putExtraString("key", "value")
intent.getExtraString("key", "default")
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 检查网络状态
if (NetworkUtil.isNetworkAvailable(this)) {
// 网络可用
}
// 获取屏幕密度
val density = DeviceUtil.getScreenDensity(this)
// 单位转换
val dpValue = UnitUtil.px2dp(this, 100)
}
}
// 字符串扩展
val text = "hello world"
if (text.isNotNullOrEmpty()) {
// 字符串不为空
}
// 列表扩展
val list = listOf(1, 2, 3)
if (list.isNotNullOrEmpty()) {
val firstItem = list.safeGet(0) // 安全获取
}
// 视图扩展
view.setVisible(isVisible)
view.setGone(!isVisible)
// 读取 Asset 文件
val content = AssetUtil.readAssetFile(context, "config.json")
// 文件压缩
val compressedImage = CompressUtil.compressImage(imagePath, 80)
// 文件拷贝
FileUtil.copyFile(sourceFile, destFile)
// 获取应用版本
val versionName = AppUtil.getVersionName(context)
val versionCode = AppUtil.getVersionCode(context)
// 检查应用安装
val isInstalled = AppUtil.isAppInstalled(context, "com.example.app")
// 获取进程名
val processName = SystemUtils.getProcessName(context)
kotlinx.coroutines - 协程支持androidx.core - Android 核心库androidx.appcompat - AppCompat 支持play-feature-delivery - Google Play 功能交付google-libphonenumber - 电话号码处理okio - IO 操作库relinker - 原生库加载external:AndroidAutoSize - 屏幕适配frame:base - 基础模块frame:data - 数据模块frame:zero - 零依赖模块implementation "com.wenext.android:frame-util:6.0.2"