一、介绍
AndroidUtilCode 是一款收录了大量常用工具类的 Android 库,全称为 Android Util Code,是 Android 平台上的工具类库。这个库里包含了 Android 开发过程中经常使用的工具方法,从开发效率、代码重用性、代码可读性等方面都具有很大的帮助。另外,AndroidUtilCode 最独到的就是将多种功能进行了封装,对 Android 开发中使用频率十分高的工具类做了很好的整合。
二、功能介绍
AndroidUtilCode 涵盖的功能之多且发杂,无法完全列举,主要包括Android框架相关、App相关、Bar相关、设备相关、Encode、File、Image、Keyboard、Lang、Log、Meta、Network、Permission、Reflect、Regex、Shell相关、Size、SPCache、Sub-process、Thread相关、Time、Toast、Zip等方面的功能。
1. Android框架相关
提供了对 Android 框架相关信息的获取和使用,例如获取 App 版本号、获取设备 UID 等。
public static String getAppVersionName(Context context) // 获取 App 版本号
public static int getAppVersionCode(Context context) // 获取 App 版本码
public static String getAndroidID() // 获取 Android 设备唯一标识
public static String getMacAddress(Context context) // 获取 Mac 地址
public static String getManufacturer() //获取设备厂商
public static String getModel() // 获取设备型号
public static String getDevice() //获取设备名
2. App相关
提供了对 App 相关信息的获取和使用,例如获取当前 App 包名、安装 App、App 启动时间等。
public static void installApp(String filePath) // 安装 App
public static boolean isAppInstalled(String packageName) // 判断 App 是否已安装
public static String getAppPackageName() // 获取当前 App 包名
public static long getAppLastInstallTime(Context context) // 获取 App 最后一次安装时间
public static long getAppFirstInstallTime(Context context) // 获取 App 第一次安装时间
public static long getAppLastUpdateTime(Context context) // 获取 App 最后一次更新时间
public static long getAppVersionCode(Context context) // 获取 App 版本码
public static String getAppVersionName(Context context) // 获取 App 版本号
public static long getAppSize(Context context) // 获取 App 安装包大小
public static boolean isAppDebug() // 判断 App 是否 Debug 版本
public static boolean isAppSystem() // 判断 App 是否为系统应用
public static boolean isAppForeground() // 判断 App 是否在前台
public static void relaunchApp() // 重启 App
public static void exitApp() // 退出 App
public static void shareText(String text, String title) // 分享文本
public static void shareImage(String filePath, String title) // 分享图片
public static void shareVideo(String filePath, String title) // 分享视频
public static void shareAudio(String filePath, String title) // 分享音频
public static void shareFile(String filePath, String title) // 分享文件
3. Bar相关
提供了对 Bar 相关信息的获取和使用,例如设置状态栏颜色和透明度。
public static void setStatusBarColor(Activity activity, int color) // 设置状态栏颜色
public static void setStatusBarColor(Activity activity, int color, boolean isDecor) //设置状态栏颜色,使用时不会影响其他 View 的布局
public static void setStatusBarAlpha(Activity activity, int alpha) // 设置状态栏透明度
public static void setStatusBarAlpha(Activity activity, int alpha, boolean isDecor) // 设置状态栏透明度,使用时不会影响其他 View 的布局
public static void setStatusBarColor4Drawer(Activity activity, DrawerLayout drawer, Toolbar toolbar, int color) // 为 DrawerLayout 设置状态栏颜色
public static void setStatusBarAlpha4Drawer(Activity activity, DrawerLayout drawer, Toolbar toolbar, int alpha) // 为 DrawerLayout 设置状态栏透明度
4. 设备相关
提供了对设备相关信息的获取和使用,例如获取设备屏幕信息、获取设备生产商、获取设备型号、获取设备宽高、获取设备序列号等。
public static boolean isDeviceRooted() // 判断设备是否有 ROOT 权限
public static String getSDKVersionName() // 获取 Android SDK 版本号
public static int getSDKVersionCode() // 获取 Android SDK 版本码
public static int getMinSdkVersion(Context context) // 获取 App 最低版本号
public static int getMaxSdkVersion(Context context) // 获取 App 最高版本号
public static String getSDKVersion() // 获取 Android SDK 版本
public static String getFingerprint() // 获取设备指纹信息
public static String getBoard() // 获取设备主板信息
public static String getBootLoader() // 获取设备引导程序版本号
public static String getBrand() // 获取设备品牌信息
public static String getDevice() // 获取设备型号信息
public static String getDisplay() // 获取设备显示信息
public static String getHardware() // 获取设备硬件信息
public static String getManufacturer() // 获取设备制造商信息
public static String getModel() // 获取手机型号信息
public static String getProduct() // 获取设备驱动信息
public static String getSerial() // 获取设备序列号(该序列号不同于 AndroidId)
public static String getAndroidId() // 获取 AndroidId
public static String getMacAddress() // 获取 Mac 地址
public static String getIPAddress(boolean useIPv4) // 获取 IP 地址
public static String getIMEI(Context context) // 获取设备 IMEI 码
public static String getIMSI(Context context) // 获取设备 IMSI 码
public static String getPhoneNumber(Context context) // 获取设备手机号
public static int getScreenDensity() // 获取设备屏幕密度(DPI)
public static float getScreenDensityScale() // 获取设备屏幕密度比率
public static float getScreenScale() // 获取设备屏幕比例
public static int getScreenWidth() // 获取设备屏幕宽度
public static int getScreenHeight() // 获取设备屏幕高度
5. Encode
提供了对编码相关信息的获取和使用,例如 MD5 加密、Base64 编码解码等。
public static byte[] hexString2Bytes(String hexString) // 十六进制字符串转为 byte 数组
public static String bytes2HexString(byte[] bytes) // byte 数组转为十六进制字符串
public static String bytes2Base64(byte[] bytes) // byte 数组转为 Base64 编码字符串
public static byte[] base642Bytes(String base64) // Base64 编码字符串转为 byte 数组(可以用于图片Base64编码)
public static String encodeMD2ToString(String data) // MD2 加密字符串
public static String encodeMD5ToString(String data) // MD5 加密字符串
public static String encodeSHA1ToString(String data) // SHA1 加密字符串
public static String encodeSHA256ToString(String data) // SHA256 加密字符串
public static String encodeSHA512ToString(String data) // SHA512 加密字符串
public static String encodeHmacMD5ToString(String data, String key) // HmacMD5 加密字符串
public static String encodeHmacSHA1ToString(String data, String key) // HmacSHA1 加密字符串
public static String encodeHmacSHA256ToString(String data, String key) // HmacSHA256 加密字符串
public static String encodeHmacSHA512ToString(String data, String key) // HmacSHA512 加密字符串
6. File
提供了对文件读取和写入、文件操作的封装,例如获取文件后缀名、获取文件大小、比较文件内容是否一致等。
public static boolean isFileExists(String filePath) // 判断文件是否存在
public static boolean writeFileFromString(String filePath, String content, boolean append) // 将字符串写入文件
public static byte[] readFile2Bytes(String filePath) // 读取指定文件内的 byte 数组
public static String readFile2String(String filePath) // 读取指定文件内的字符串
public static boolean isFileEquals(File file1, File file2) // 判断两个文件是否相等
public static boolean isFileEquals(String filePath1, String filePath2) // 判断两个文件是否相等
public static boolean copyFile(String sourceFilePath, String destFilePath) // 将原文件复制到目标文件
public static boolean copyDir(String sourceDirPath, String destDirPath) // 将原文件夹复制到目标文件夹
public static boolean moveFile(String sourceFilePath, String destFilePath) // 将原文件移动到目标文件
public static boolean moveDir(String sourceDirPath, String destDirPath) // 将原文件夹移动到目标文件夹
public static boolean deleteFile(String filePath) // 删除文件
public static boolean deleteDir(String dirPath) // 删除文件夹
public static long getFileSize(String filePath) // 获取文件大小
public static long getDirSize(String dirPath) // 获取文件夹大小
public static String[] getDirName(String dirPath) // 获取文件夹名称
public static String[] getDirFiles(String dirPath) // 获取文件夹内的文件名数组
public static String[] getDirPaths(String dirPath) // 获取文件夹内的文件路径数组
public static String getFileName(String filePath) // 获取文件名
public static String getFileNameWithSuffix(String filePath) // 获取文件名(带后缀)
public static String getFileSuffix(String filePath) // 获取文件后缀名
public static String getDirName(String filePath) // 获取文件所在文件夹的名称
public static String getAbsolutePath(File file) // 获取文件绝对路径
7. Image
提供了对图片处理的封装,例如图片压缩、图片转换等。
public static boolean save(Bitmap src, String filePath, Bitmap.CompressFormat format, boolean recycle) // 保存图片
public static Bitmap compressByScale(Bitmap src, float scale, float quality, Bitmap.Config config, boolean recycle) // 按比例缩小图片尺寸
public static Bitmap compressByScale(Bitmap src, float scale, Bitmap.Config config, boolean recycle) // 按比例缩小图片尺寸
public static Bitmap compressByQuality(Bitmap src, int quality, Bitmap.Config config, boolean recycle) // 按质量压缩图片
public static Bitmap compressBySampleSize(Bitmap src, int sampleSize, Bitmap.Config config, boolean recycle) // 按采样率缩小图片尺寸
public static Bitmap rotate(Bitmap src, int degrees, float px, float py, boolean recycle) // 旋转图片
public static Bitmap rotate(Bitmap src, int degrees, boolean recycle) // 旋转图片
public static Bitmap toBitmap(Drawable drawable, int width, int height, Bitmap.Config config) // Drawable 转为 Bitmap
public static byte[] bitmap2Bytes(Bitmap src, Bitmap.CompressFormat format) // Bitmap 转为 byte 数组
public static Bitmap bytes2Bitmap(byte[] bytes) // Byte 数组转为 Bitmap
public static Bitmap drawable2Bitmap(Drawable drawable) // Drawable 转为 Bitmap
public static byte[] drawable2Bytes(Drawable drawable, Bitmap.CompressFormat format) // Drawable 转为 byte 数组
public static Drawable bytes2Drawable(byte[] bytes) // Byte 数组转为 Drawable
public static Drawable bitmap2Drawable(Bitmap bitmap) // Bitmap 转为 Drawable
public static byte[] bitmap2Bytes(Bitmap bitmap) // Bitmap 转为 byte 数组
public static Bitmap file2Bitmap(String filePath) // 文件转为 Bitmap
public static Drawable file2Drawable(String filePath) // 文件转为 Drawable
public static boolean save(Bitmap src, String filePath, Bitmap.CompressFormat format, boolean recycle) // 保存图片
public static int getImageWidth(String filePath) // 获取图片宽度
public static int getImageHeight(String filePath) // 获取图片高度
public static String compressBySampleSize(String srcFilePath, String destFilePath, int inSampleSize) // 通过采样率压缩图片
public static String compressByQuality(String srcFilePath, String destFilePath, int quality) // 通过质量压缩图片
public static String compressByScale(String srcFilePath, String destFilePath, float scale) // 通过缩放比例压缩图片
public static String compressByMaxSize(String srcFilePath, String destFilePath, int maxWidth, int maxHeight) // 通过最大宽高压缩图片
public static String compressByMinSize
原创文章,作者:VHFIE,如若转载,请注明出处:https://www.506064.com/n/369523.html