---
title: "Android常用的工具类"
author: "Trinea"
date: "2013-10-12T09:09:40Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2014-01-05T10:20:29Z"
id: 1955581
url: "https://trinea.iteye.com/blog/1955581"
---

**最新最准确内容建议直接访问原文：[Android常用的工具类](http://www.trinea.cn/android/android-common-utils/)**

 

主要介绍**总结的Android开发中常用的工具类，大部分同样适用于Java**。  
目前包括HttpUtils、DownloadManagerPro、ShellUtils、PackageUtils、PreferencesUtils、JSONUtils、FileUtils、ResourceUtils、StringUtils、ParcelUtils、RandomUtils、ArrayUtils、ImageUtils、ListUtils、MapUtils、ObjectUtils、SerializeUtils、SystemUtils、TimeUtils。

The English version of this article see：[Android Common Utils](http://www.trinea.cn/android/android-common-utils-en "English Version")

 

所有代码都在[TrineaAndroidCommon@Github](https://github.com/Trinea/AndroidCommon "TrineaAndroidCommon项目Github上开源地址")中，欢迎Star或Fork^\_\*，除这些工具类外此项目还包括[缓存、下拉ListView](http://www.trinea.cn/android/android%e5%85%ac%e5%85%b1%e5%ba%93%e7%bc%93%e5%ad%98-%e4%b8%8b%e6%8b%89listview-%e4%b8%8b%e8%bd%bd%e7%ae%a1%e7%90%86pro-%e9%9d%99%e9%bb%98%e5%ae%89%e8%a3%85-root%e8%bf%90%e8%a1%8c-java%e5%85%ac/ "Android公共库(缓存 下拉ListView 下载管理Pro 静默安装 root运行 Java公共类)")等。详细接口介绍可见[TrineaAndroidCommon API Guide](http://trinea.github.io/doc/trinea_android_common/index.html)。

示例APK：[TrineaAndroidDemo](https://play.google.com/store/apps/details?id=cn.trinea.android.demo)

具体使用：可直接引入[TrineaAndroidCommon](https://github.com/Trinea/AndroidCommon "TrineaAndroidCommon项目Github上开源地址")作为你项目的library[(如何拉取代码及添加公共库)](http://www.trinea.cn/android/%e6%b7%bb%e5%8a%a0android-common-lib%e6%b7%bb%e5%8a%a0%e5%8f%8agoogle-code%e5%92%8cgithub%e5%a6%82%e4%bd%95%e6%8b%89%e5%8f%96%e4%bb%a3%e7%a0%81/ "Github和Google Code拉取代码及如何添加Android Common Lib")，或是自己抽取其中的部分使用。

 

**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](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/HttpUtils.java)，更多方法及更详细参数介绍可见[HttpUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/HttpUtils.html)。

 

**2、DownloadManagerPro**  
Android系统下载管理DownloadManager增强方法，可用于包括获取下载相关信息，如：  
getStatusById(long) 得到下载状态  
getDownloadBytes(long) 得到下载进度信息  
getBytesAndStatus(long) 得到下载进度信息和状态  
getFileName(long) 得到下载文件路径  
getUri(long) 得到下载uri  
getReason(long) 得到下载失败或暂停原因  
getPausedReason(long) 得到下载暂停原因  
getErrorCode(long) 得到下载错误码  
源码可见[DownloadManagerPro.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/DownloadManagerPro.java)，更多方法及更详细参数介绍可见[DownloadManagerPro Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/DownloadManagerPro.html)。关于Android DownManager使用可见[DownManager Demo](http://www.trinea.cn/android/android%e7%b3%bb%e7%bb%9f%e4%b8%8b%e8%bd%bd%e7%ae%a1%e7%90%86downloadmanager%e5%8a%9f%e8%83%bd%e4%bb%8b%e7%bb%8d%e5%8f%8a%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b/ "Android系统下载管理DownloadManager功能介绍及使用示例")。

 

**3、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](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ShellUtils.java)，更多方法及更详细参数介绍可见[ShellUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ShellUtils.html)。关于静默安装可见[apk-root权限静默安装](http://www.trinea.cn/android/android%e5%b8%b8%e7%94%a8%e4%bb%a3%e7%a0%81%e4%b9%8bapk-root%e6%9d%83%e9%99%90%e9%9d%99%e9%bb%98%e5%ae%89%e8%a3%85/ "Android常用代码之APK root权限静默安装")。

 

**4、PackageUtils**  
Android包相关工具类，可用于(root)安装应用、(root)卸载应用、判断是否系统应用等，如：  
install(Context, String) 安装应用，如果是系统应用或已经root，则静默安装，否则一般安装  
uninstall(Context, String) 卸载应用，如果是系统应用或已经root，则静默卸载，否则一般卸载  
isSystemApplication(Context, String) 判断应用是否为系统应用  
源码可见[PackageUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/PackageUtils.java)，更多方法及更详细参数介绍可见[ShellUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/PackageUtils.html)。关于静默安装可见[apk-root权限静默安装](http://www.trinea.cn/android/android%e5%b8%b8%e7%94%a8%e4%bb%a3%e7%a0%81%e4%b9%8bapk-root%e6%9d%83%e9%99%90%e9%9d%99%e9%bb%98%e5%ae%89%e8%a3%85/ "Android常用代码之APK root权限静默安装")。

 

**5、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](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/PreferencesUtils.java)，更多方法及更详细参数介绍可见[PreferencesUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/PreferencesUtils.html)。

 

**6、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](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/JSONUtils.java)，更多方法及更详细参数介绍可见[JSONUtils Api Guide](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/JSONUtils.java)。

 

**7、FileUtils**  
文件工具类，可用于读写文件及对文件进行操作。如：  
readFile(String filePath) 读文件  
writeFile(String filePath, String content, boolean append) 写文件  
getFileSize(String path) 得到文件大小  
deleteFile(String path) 删除文件  
源码可见[FileUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/FileUtils.java)，更多方法及更详细参数介绍可见[FileUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/FileUtils.html)。

 

**8、ResourceUtils**  
Android Resource工具类，可用于从android资源目录的raw和assets目录读取内容，如：  
geFileFromAssets(Context context, String fileName) 得到assets目录下某个文件内容  
geFileFromRaw(Context context, int resId) 得到raw目录下某个文件内容  
源码可见[ResourceUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ResourceUtils.java)，更多方法及更详细参数介绍可见[ResourceUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ResourceUtils.html)。

 

**9、StringUtils**  
String工具类，可用于常见字符串操作，如：  
isEmpty(String str) 判断字符串是否为空或长度为0  
isBlank(String str) 判断字符串是否为空或长度为0 或由空格组成  
utf8Encode(String str) 以utf-8格式编码  
capitalizeFirstLetter(String str) 首字母大写  
源码可见[StringUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/StringUtils.java)，更多方法及更详细参数介绍可见[StringUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/StringUtils.html)。

 

**10、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&lt;K, V&gt; map, Parcel out, int flags) 向parcel中写入map类型数据  
源码可见[ParcelUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ParcelUtils.java)，更多方法及更详细参数介绍可见[ParcelUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ParcelUtils.html)。

 

**11、RandomUtils**  
随机数工具类，可用于获取固定大小固定字符内的随机数，如：  
getRandom(char\[] sourceChar, int length) 生成随机字符串，所有字符均在某个字符串内  
getRandomNumbers(int length) 生成随机数字  
源码可见[RandomUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/RandomUtils.java)，更多方法及更详细参数介绍可见[RandomUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/RandomUtils.html)。

 

**12、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](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ArrayUtils.java)，更多方法及更详细参数介绍可见[ArrayUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ArrayUtils.html)。

 

**13、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](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ImageUtils.java)，更多方法及更详细参数介绍可见[ImageUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ImageUtils.html)。

 

**14、ListUtils**  
List工具类，可用于List常用操作，如：  
isEmpty(List&lt;V&gt; sourceList) 判断List是否为空或长度为0  
join(List&lt;String&gt; list, String separator) List转换为字符串，并以固定分隔符分割  
addDistinctEntry(List&lt;V&gt; sourceList, V entry) 向list中添加不重复元素  
源码可见[ListUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ListUtils.java)，更多方法及更详细参数介绍可见[ListUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ListUtils.html)。

 

**15、MapUtils**  
Map工具类，可用于Map常用操作，如：  
isEmpty(Map&lt;K, V&gt; sourceMap) 判断map是否为空或长度为0  
parseKeyAndValueToMap(String source, String keyAndValueSeparator, String keyAndValuePairSeparator, boolean ignoreSpace) 字符串解析为map  
toJson(Map&lt;String, String&gt; map) map转换为json格式  
源码可见[MapUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/MapUtils.java)，更多方法及更详细参数介绍可见[MapUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/MapUtils.html)。

 

**16、ObjectUtils**  
Object工具类，可用于Object常用操作，如：  
isEquals(Object actual, Object expected) 比较两个对象是否相等  
compare(V v1, V v2) 比较两个对象大小  
transformIntArray(int\[] source)  Integer 数组转换为int数组  
源码可见[ObjectUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ObjectUtils.java)，更多方法及更详细参数介绍可见[ObjectUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ObjectUtils.html)。

 

**17、SerializeUtils**  
序列化工具类，可用于序列化对象到文件或从文件反序列化对象，如：  
deserialization(String filePath) 从文件反序列化对象  
serialization(String filePath, Object obj) 序列化对象到文件  
源码可见[SerializeUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/SerializeUtils.java)，更多方法及更详细参数介绍可见[SerializeUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/SerializeUtils.html)。

 

**18、SystemUtils**  
系统信息工具类，可用于得到线程池合适的大小，目前功能薄弱，后面会进行增强。如：  
getDefaultThreadPoolSize() 得到跟系统配置相符的线程池大小  
源码可见[SystemUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/SystemUtils.java)，更多方法及更详细参数介绍可见[SystemUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/SystemUtils.html)。

 

**19、TimeUtils**  
时间工具类，可用于时间相关操作，如：  
getCurrentTimeInLong() 得到当前时间  
getTime(long timeInMillis, SimpleDateFormat dateFormat) 将long转换为固定格式时间字符串  
源码可见[TimeUtils.java](https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/TimeUtils.java)，更多方法及更详细参数介绍可见[TimeUtils Api Guide](http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/TimeUtils.html)。

 

**你可能还感兴趣：**

[Android性能优化系列总篇](http://www.trinea.cn/android/performance/ "性能优化系列总篇")

[Android公共库(缓存 下拉ListView 下载管理Pro 静默安装 root运行 Java公共类)](http://www.trinea.cn/android/android%e5%85%ac%e5%85%b1%e5%ba%93%e7%bc%93%e5%ad%98-%e4%b8%8b%e6%8b%89listview-%e4%b8%8b%e8%bd%bd%e7%ae%a1%e7%90%86pro-%e9%9d%99%e9%bb%98%e5%ae%89%e8%a3%85-root%e8%bf%90%e8%a1%8c-java%e5%85%ac/ "Android公共库(缓存 下拉ListView 下载管理Pro 静默安装 root运行 Java公共类)")

[Android ImageCache图片缓存](http://www.trinea.cn/android/android-imagecache%e5%9b%be%e7%89%87%e7%bc%93%e5%ad%98%ef%bc%8c%e4%bd%bf%e7%94%a8%e7%ae%80%e5%8d%95%ef%bc%8c%e6%94%af%e6%8c%81%e9%a2%84%e5%8f%96%ef%bc%8c%e6%94%af%e6%8c%81%e5%a4%9a%e7%a7%8d%e7%bc%93/ "Android ImageCache图片缓存，使用简单，支持预取，支持多种缓存算法，支持不同网络类型，扩展性强")