首页 > 其他分享 >Android常用的工具类

Android常用的工具类

时间:2023-08-03 12:34:34浏览次数:31  
标签:常用 github cn url 工具 common trinea Android 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]。

标签:常用,github,cn,url,工具,common,trinea,Android,android
From: https://blog.51cto.com/u_15955464/6947992

相关文章

  • 深入浅出RxJava (四:在Android中使用响应式编程)
    [url=http://blog.danlew.net/2014/10/08/grokking-rxjava-part-4/]原文链接[/url]在第1,2,3篇中,我大概介绍了RxJava是怎么使用的。下面我会介绍如何在Android中使用RxJava.RxAndroidRxAndroid是RxJava的一个针对Android平台的扩展。它包含了一些能够简化And......
  • 除了Copilot还有这些AI代码辅助工具
    最近牛逼的GitHubCopilot试用到期了,离开它还有点不习惯,基础的代码它基本可以帮你搞定,开发效率直接翻倍。为啥这么好用,Copilot的背后是OpenAI和强大的GitHub代码库。那么有没有可以取代它而免费IDE的AI代码辅助工具呢?还真有,小编本文介绍几种免费的代码智能辅助工具。1、bito.aiB......
  • 酷狗.KGM 转码工具
     {  https://github.com/ghtz08/kuguo-kgm-decoder}尊重开源精神{Copyright(c)<year><copyrightholders>Anti996LicenseVersion1.0(Draft)Permissionisherebygrantedtoanyindividualorlegalentityobtainingacopyofthislicensedwork......
  • 方便在非spring管理环境中获取bean的spring工具类
    spring工具类方便在非spring管理环境中获取beanimportorg.springframework.aop.framework.AopContext;importorg.springframework.beans.BeansException;importorg.springframework.beans.factory.NoSuchBeanDefinitionException;importorg.springframework.beans.facto......
  • android开发Android studio卡顿配置studio64.exe.vmoptions文件的解决方法
    备份一下studio64.exe.vmoptions写法:-Xms2g-Xmx16g-XX:ReservedCodeCacheSize=2g-XX:+IgnoreUnrecognizedVMOptions-XX:+UseG1GC-XX:SoftRefLRUPolicyMSPerMB=100-XX:CICompilerCount=2-XX:+HeapDumpOnOutOfMemoryError-XX:-OmitStackTraceInFastThrow-da-Djna.nosys=t......
  • Linux环境下,使用远程连接工具过程中终端无法弹出图形窗口
    Linux操作系统,请通过管理网口/VNC等远程连接工具登录服务器,使用终端执行操作。如果在使用过程中无法弹出图形窗口,请执行以下操作。场景一:使用SSH连接工具登录LINUX服务器确保工具支持远程图形显示。SSH连接工具需要支持远程图形显示,才能弹出图形窗口。putty、SecureCRT默认......
  • StoneDB 源码解读系列|Tianmu 引擎工具类模块源码详解(一)
    StoneDB源码解读系列文章正式开启,预计以周更的形式跟大家见面,请多多支持~本篇源码解读内容已进行直播分享,可在视频号观看直播回放,也可点击阅读原文跳转至B站观看回放视频。PPT内容可在社区论坛中查看下载:https://forum.stonedb.io/t/topic/89各个工具类属于Tianmu引擎的......
  • 常用c++ STL 汇总
    常用STL:vector变长数组,倍增的思想初始化://初始化vector<int>a;vector<int>a(n);vector<int>a[n];vector<int>a(n,0);//长度为n,值为0操作:size()//返回元素个数empty()//返回是否为空clear()//清空front()/back()//返回第一个/最后一个元素push_bac......
  • Downie 4 Mac好用的视频下载工具
    Downie4是一款Mac上的下载工具,它支持macOS和iOS系统。可以下载电影,音乐,视频,播客以及其他内容。Downie4允许您在Mac上进行下载。它可以让你下载您最喜欢的视频和音乐。在你的应用程序中查找要下载的内容后,请将其从应用程序上删除,然后开始在Downie4上搜索和找到它。→→......
  • postgresql 常用的删除重复数据方法
    一、最高效方法测试环境验证,6600万行大表,删除2200万重复数据仅需3分钟deletefromdeltestawherea.ctid=any(array(selectctidfrom(selectrow_number()over(partitionbyid),ctidfromdeltest)twheret.row_number>1)); --family_no相同的数据,保留i......