Android常用的工具类

主要介绍总结的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:[url=http://www.trinea.cn/android/android-common-utils-en/]Android Common Utils[/url]



所有代码都在[url=https://github.com/Trinea/AndroidCommon]TrineaAndroidCommon@Github[/url]中,欢迎Star或Fork^_*,除这些工具类外此项目还包括[url=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/]缓存、下拉ListView[/url]等。详细接口介绍可见[url=http://trinea.github.io/doc/trinea_android_common/index.html]TrineaAndroidCommon API Guide[/url]。

具体使用:可直接引入[url=https://github.com/Trinea/AndroidCommon]TrineaAndroidCommon[/url]作为你项目的library[url=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/](如何拉取代码及添加公共库)[/url],或是自己抽取其中的部分使用。



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



2、DownloadManagerPro
Android系统下载管理DownloadManager增强方法,可用于包括获取下载相关信息,如:
getStatusById(long) 得到下载状态
getDownloadBytes(long) 得到下载进度信息
getBytesAndStatus(long) 得到下载进度信息和状态
getFileName(long) 得到下载文件路径
getUri(long) 得到下载uri
getReason(long) 得到下载失败或暂停原因
getPausedReason(long) 得到下载暂停原因
getErrorCode(long) 得到下载错误码
源码可见[url=https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/DownloadManagerPro.java]DownloadManagerPro.java[/url],更多方法及更详细参数介绍可见[url=http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/DownloadManagerPro.html]DownloadManagerPro Api Guide[/url]。关于Android DownManager使用可见[url=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/]DownManager Demo[/url]。



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环境执行命令
源码可见[url=https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ShellUtils.java]ShellUtils.java[/url],更多方法及更详细参数介绍可见[url=http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ShellUtils.html]ShellUtils Api Guide[/url]。关于静默安装可见[url=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/]apk-root权限静默安装[/url]。



4、PackageUtils
Android包相关工具类,可用于(root)安装应用、(root)卸载应用、判断是否系统应用等,如:
install(Context, String) 安装应用,如果是系统应用或已经root,则静默安装,否则一般安装
uninstall(Context, String) 卸载应用,如果是系统应用或已经root,则静默卸载,否则一般卸载
isSystemApplication(Context, String) 判断应用是否为系统应用
源码可见[url=https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/PackageUtils.java]PackageUtils.java[/url],更多方法及更详细参数介绍可见[url=http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/PackageUtils.html]ShellUtils Api Guide[/url]。关于静默安装可见[url=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/]apk-root权限静默安装[/url]。



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



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的值
源码可见[url=https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/JSONUtils.java]JSONUtils.java[/url],更多方法及更详细参数介绍可见[url=https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/JSONUtils.java]JSONUtils Api Guide[/url]。



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



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



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



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



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



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表示是否循环
源码可见[url=https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ArrayUtils.java]ArrayUtils.java[/url],更多方法及更详细参数介绍可见[url=http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ArrayUtils.html]ArrayUtils Api Guide[/url]。



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) 缩放图片
源码可见[url=https://github.com/Trinea/AndroidCommon/blob/master/src/cn/trinea/android/common/util/ImageUtils.java]ImageUtils.java[/url],更多方法及更详细参数介绍可见[url=http://trinea.github.io/doc/trinea_android_common/cn/trinea/android/common/util/ImageUtils.html]ImageUtils Api Guide[/url]。



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



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



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



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



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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值