10/08
2013

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

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

10 thoughts on “Android Common Utils

  1. Pingback: Android常用的工具类 - 有Bug

  2. Pingback: 总结的Android开发中常用的工具类 | 学编程网

  3. Pingback: Android常用工具类 | 思考的码农

  4. Pingback: Youth Fringe | Android常用工具类

  5. Pingback: Android开发中常用的工具类 - 移动端开发 - 开发者

  6. Pingback: 狙击手 » Android常用工具类

  7. Pingback: 别路寻忆 » Android常用工具类【转发】

  8. Pingback: Android常用的工具类 | 吕品的博客