Android Common Utils

简介:

This topic describes utils class commonly used in Android development, most of them same applies to Java.
Currently includes HttpUtils、DownloadManagerPro、ShellUtils、PackageUtils、PreferencesUtils、JSONUtils、FileUtils、ResourceUtils、StringUtils、ParcelUtils、RandomUtils、ArrayUtils、ImageUtils、ListUtils、MapUtils、ObjectUtils、SerializeUtils、SystemUtils、TimeUtils。

本文中文版见:Android常用的工具类

All code in TrineaAndroidCommon@Github. Welcome Star or Fork ^ _ *, in addition to including these tools also included the cache, dropListView and so on. Detailed description of interface see: TrineaAndroidCommon API Guide.

Usage: can be directly introduced TrineaAndroidCommon as your project’s library (How to pull the code and add the public library), or part of them to use their extraction.

1、HttpUtils
Http network utils, include httpGet、httpPost and function of http para,for example with httpGet:
static HttpResponse httpGet(HttpRequest request)
static HttpResponse httpGet(java.lang.String httpUrl)
static String httpGetString(String httpUrl)
three functions above, use gzip default, use bufferedReader improve reading speed. You can set url, timeout, userAgent or other paras to HttpRequest. You can get reponse body, http response code, http expires(max-age in Cache-Control and expires) from HttpResponse.
Source code see: HttpUtils.java, Detailed description of interface see:HttpUtils Api Guide

2、DownloadManagerPro
Pro fro android downloadManager, can be used to get download info, for exmaple:
getStatusById(long) get download status
getDownloadBytes(long) get downloaded byte, total byte
getBytesAndStatus(long) get downloaded byte, total byte and download status
getFileName(long) get download file name
getUri(long) get download uri
getReason(long) get failed code or paused reason
getPausedReason(long) get paused reason
getErrorCode(long) get failed error code
Source code see: DownloadManagerPro.java,Detailed description of interface see:DownloadManagerPro Api Guide

3、ShellUtils
Android Shell Utils, can be used to check root permission, run command in root or shell permission, for exmaple:
checkRootPermission() check root permission
execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) execute shell commands, second paras means whether need to run with root
execCommand(String command, boolean isRoot)
Source code see: ShellUtils.java,Detailed description of interface see:ShellUtils Api Guide

4、PackageUtils
Android package utils, can be used install(uninstall) package silent, check whether system app, for exmaple:
install(Context, String) if system application or rooted, install silent or uninstall normal
uninstall(Context, String) if system application or rooted, uninstall silent or uninstall normal
isSystemApplication(Context, String) whether packageName is system application
Source code see: PackageUtils.java,Detailed description of interface see:ShellUtils Api Guide

5、PreferencesUtils
Android SharedPreferences Utils, can be used to put and get data from SharedPreferences, for exmaple:
putString(Context, String, String) put string preferences
putInt(Context, String, int) put int preferences
getString(Context, String) get string preferences
getInt(Context, String) put int preferences
u can set preference name by modify value of PREFERENCE_NAME
Source code see: PreferencesUtils.java,Detailed description of interface see:PreferencesUtils Api Guide

6、JSONUtils
JSONUtils Utils, can be used to get data from json, for exmaple:
String getString(JSONObject jsonObject, String key, String defaultValue)
String getString(String jsonData, String key, String defaultValue)
get string from json

getMap(JSONObject jsonObject, String key)
getMap(String jsonData, String key)
get map from json
Source code see: JSONUtils.java,Detailed description of interface see:JSONUtils Api Guide

7、FileUtils
File Utils, can be used to read file, write file or operate file, for exmaple:
readFile(String filePath) read content from file
writeFile(String filePath, String content, boolean append) write content to file
getFileSize(String path) get file size
deleteFile(String path) delete file
Source code see: FileUtils.java,Detailed description of interface see:FileUtils Api Guide

8、ResourceUtils
Android Resource Utils, can be used to read content from raw or assets for exmaple:
geFileFromAssets(Context context, String fileName) get an asset
geFileFromRaw(Context context, int resId) get content from a raw resource
Source code see: ResourceUtils.java,Detailed description of interface see:ResourceUtils Api Guide

9、StringUtils
String Utils, can be used to do common string operate, for exmaple:
isEmpty(String str) whether string is null or empty
isBlank(String str) whether string is null or its length is 0 or it is made by space
utf8Encode(String str) encoded in utf-8
capitalizeFirstLetter(String str) capitalize first letter
Source code see: StringUtils.java,Detailed description of interface see:StringUtils Api Guide

10、ParcelUtils
Android Parcel Utils, can be used to read or write special data, for exmaple:
readBoolean(Parcel in)
readHashMap(Parcel in, ClassLoader loader)
writeBoolean(boolean b, Parcel out)
writeHashMap(Map<K, V> map, Parcel out, int flags)
Source code see: ParcelUtils.java,Detailed description of interface see:ParcelUtils Api Guide

11、RandomUtils
Random utils, can be used to generate ramdom data, for exmaple:
getRandom(char[] sourceChar, int length)
getRandomNumbers(int length)
Source code see: RandomUtils.java,Detailed description of interface see:RandomUtils Api Guide

12、ArrayUtils
Array Utils, can be used to do common array operate, for exmaple:
isEmpty(V[] sourceArray) whether array is null or empty
getLast(V[] sourceArray, V value, V defaultValue, boolean isCircle) get last element of the target element
getNext(V[] sourceArray, V value, V defaultValue, boolean isCircle) get next element of the target element
Source code see: ArrayUtils.java,Detailed description of interface see:ArrayUtils Api Guide

13、ImageUtils
Image Utils, can be used to convert between Bitmap, byte array, Drawable or scale image, for exmaple:
bitmapToDrawable(Bitmap b)
drawableToBitmap(Drawable d)
drawableToByte(Drawable d)
scaleImage(Bitmap org, float scaleWidth, float scaleHeight)
Source code see: ImageUtils.java,Detailed description of interface see:ImageUtils Api Guide

14、ListUtils
List Utils, can be used to do common list operate, for exmaple:
isEmpty(List<V> sourceList) whether list is null or empty
join(List<String> list, String separator) join list to string
addDistinctEntry(List<V> sourceList, V entry) add distinct entry to list
Source code see: ListUtils.java,Detailed description of interface see:ListUtils Api Guide

15、MapUtils
map Utils, can be used to do common map operate, for exmaple:
isEmpty(Map<K, V> sourceMap) whether map is null or empty
parseKeyAndValueToMap(String source, String keyAndValueSeparator, String keyAndValuePairSeparator, boolean ignoreSpace) parse key-value pairs to map, ignore empty key
toJson(Map<String, String> map)
Source code see: MapUtils.java,Detailed description of interface see:MapUtils Api Guide

16、ObjectUtils
Object Utils, can be used to do common object operate, for exmaple:
isEquals(Object actual, Object expected) compare two object
compare(V v1, V v2) compare two object
transformIntArray(int[] source) convert int array to Integer array
Source code see: ObjectUtils.java,Detailed description of interface see:ObjectUtils Api Guide

17、SerializeUtils
Serialize Utils, can be used to deserialization object from file or serialize object to file, for exmaple:
deserialization(String filePath) deserialization from file
serialization(String filePath, Object obj) serialize to file
Source code see: SerializeUtils.java,Detailed description of interface see:SerializeUtils Api Guide

18、SystemUtils
System Utils, can be used to get get recommend default thread pool size, for exmaple:
getDefaultThreadPoolSize() get recommend default thread pool size
Source code see: SystemUtils.java,Detailed description of interface see:SystemUtils Api Guide

19、TimeUtils
Time Utils, can be used to do comon time operate, for exmaple:
getCurrentTimeInLong() get current time in milliseconds
getTime(long timeInMillis, SimpleDateFormat dateFormat) long time to string
Source code see: TimeUtils.java,Detailed description of interface see:TimeUtils Api Guide

目录
相关文章
|
Android开发
Android can-utils
Android can-utils
245 0
错误: 程序包android.support.v7.app不存在 import android.support.v7.app.AppCompatActivity
错误: 程序包android.support.v7.app不存在 import android.support.v7.app.AppCompatActivity
433 0
android.support.v4.app.FragmentManager无法转换为android.app.FragmentManager
android.support.v4.app.FragmentManager无法转换为android.app.FragmentManager
146 0
android.support.v4.app.FragmentManager无法转换为android.app.FragmentManager
android.support.v4.app.Fragment$InstantiationException问题解决
android.support.v4.app.Fragment$InstantiationException问题解决
|
API Android开发 开发工具
(转)Android Studio Error:Failed to resolve: com.android.support:appcompat-v7:25.1.0解决方案
今天不知道为什么导入eclipse项目后就出现了错误,没导入之前是正常使用AS的 Error:(26, 13) Failed to resolve: com.android.support:appcompat-v7:25.+ 第一、点击Install Repository 。
3451 0
|
Android开发 数据安全/隐私保护 安全
Android Smack_4_2_0.jar 使用
Android Studio依赖或者官网下载 // XMPP通信 compile 'org.igniterealtime.smack:smack-im:4.
1187 0
|
Android开发
Android 解决Could not find com.android.tools.build:gradle
异常 Error:Could not find com.android.tools.build:gradle:2.2.2. Searched in the following locations: https://repo1.
7035 0