Mainly introduces and summarizes the commonly used tool classes in Android development, most of which are also applicable to Java.
Currently includes HttpUtils, DownloadManagerPro, ShellUtils, PackageUtils, PreferencesUtils, JSONUtils, FileUtils, ResourceUtils, StringUtils, ParcelUtils, RandomUtils, ArrayUtils, ImageUtils, ListUtils, MapUtils, ObjectUtils, SerializeUtils, SystemUtils, TimeUtil s.
The English version of this article see: Android Common Utils
All codes are in TrineaAndroidCommon@Github. Star or Fork^_* are welcome. In addition to these tool classes, this project also includes caching, drop-down ListView, etc. Detailed interface introduction can be found in TrineaAndroidCommon API Guide.
Specific use: You can directly introduce TrineaAndroidCommon as the library of your project (how to pull the code and add public libraries), or extract part of it yourself for use.
1. HttpUtils
Http network tool class, mainly including httpGet, httpPost and http parameter related methods, taking httpGet as an example:
static HttpResponse httpGet(HttpRequest request)
static HttpResponse httpGet(java.lang.String httpUrl)
static String httpGetString(String httpUrl)
Contains the above three methods, uses gzip compression by default, and uses bufferedReader to improve reading speed.
Other http parameters such as url, timeout, userAgent, etc. can be set in HttpRequest
In HttpResponse, you can get the return content, http response code, http expiration time (max-age and expires of Cache-Control), etc.
The first two methods can perform advanced parameter settings and return rich content. The third method can simply pass in the URL to obtain the returned content, similar to httpPost. For more detailed settings, you can use HttpURLConnection or apache's HttpClient directly.
The source code can be found in HttpUtils.java, and more methods and more detailed parameter introductions can be found in the HttpUtils Api Guide.
2. DownloadManagerPro
Android system download management DownloadManager enhanced method, which can be used to obtain download-related information, such as:
getStatusById(long) Get download status
getDownloadBytes(long) Get download progress information
getBytesAndStatus(long) Get download progress information and status
getFileName(long) Get the download file path
getUri(long) gets the download uri
getReason(long) Get the reason for download failure or suspension
getPausedReason(long) Get the download pause reason
getErrorCode(long) Get download error code
The source code can be found in DownloadManagerPro.java, and more methods and detailed parameter introduction can be found in the DownloadManagerPro Api Guide. About Android DownManager use visible DownManager Demo.
3. ShellUtils
Android Shell tool class, which can be used to check system root permissions and execute shell commands under the shell or root user. Such as:
checkRootPermission() Check root permission
execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) executes commands in the shell environment. The second parameter indicates whether to execute with root permissions
execCommand(String command, boolean isRoot) shell environment execution command
The source code can be found in ShellUtils.java, and more methods and more detailed parameter introductions can be found in the ShellUtils Api Guide. Regarding silent installation, see silent installation with apk-root permission.
4. PackageUtils
Android package related tools can be used to (root) install applications, (root) uninstall applications, determine whether system applications, etc., such as:
install(Context, String) installs the application. If it is a system application or has been rooted, it will be installed silently, otherwise it will be installed normally
uninstall(Context, String) Uninstall the application. If it is a system application or has been rooted, it will be uninstalled silently, otherwise it will be uninstalled normally
isSystemApplication(Context, String) determines whether the application is a system application
The source code can be found in PackageUtils.java, and more methods and more detailed parameter introductions can be found in the ShellUtils Api Guide. Regarding silent installation, see silent installation with apk-root permission.
5. PreferencesUtils
Android SharedPreferences related tool classes can be used to conveniently read and write related types of data to SharedPreferences, such as:
putString(Context, String, String) save string type data
putInt(Context, String, int) save int type data
getString(Context, String) Get string type data
getInt(Context, String) Get int type data
The preference name can be modified by modifying the PREFERENCE_NAME variable
The source code can be found in PreferencesUtils.java, and more methods and more detailed parameter introductions can be found in the PreferencesUtils Api Guide.
6. JSONUtils
JSONUtils tool class can be used to conveniently read and write related types of data into Json, such as:
String getString(JSONObject jsonObject, String key, String defaultValue) gets string type value
String getString(String jsonData, String key, String defaultValue) Get string type value
Represents reading the value of a String type key from json
getMap(JSONObject jsonObject, String key) get map
getMap(String jsonData, String key) get map
Represents reading the value of a certain Map type key from json
The source code can be found in JSONUtils.java, and more methods and more detailed parameter introductions can be found in the JSONUtils Api Guide.
7. FileUtils
File tool class, which can be used to read, write and operate files. Such as:
readFile(String filePath) read file
writeFile(String filePath, String content, boolean append) write file
getFileSize(String path) gets the file size
deleteFile(String path) delete file
The source code can be found in FileUtils.java, and more methods and more detailed parameter introductions can be found in the FileUtils Api Guide.
8. ResourceUtils
Android Resource tool class, which can be used to read content from the raw and assets directories of the android resource directory, such as:
geFileFromAssets(Context context, String fileName) gets the content of a file in the assets directory
geFileFromRaw(Context context, int resId) gets the content of a file in the raw directory
The source code can be found in ResourceUtils.java, and more methods and more detailed parameter introductions can be found in the ResourceUtils Api Guide.
9. StringUtils
String tool class, which can be used for common string operations, such as:
isEmpty(String str) determines whether the string is empty or has a length of 0
isBlank(String str) determines whether the string is empty or has a length of 0 or consists of spaces
utf8Encode(String str) encodes in utf-8 format
capitalizeFirstLetter(String str) Capitalize the first letter
The source code can be found in StringUtils.java, and more methods and more detailed parameter introductions can be found in the StringUtils Api Guide.
10. ParcelUtils
Android Parcel tool class, which can be used to read or write special types of data from parcel, such as:
readBoolean(Parcel in) Read boolean type data from packel
readHashMap(Parcel in, ClassLoader loader) reads map type data from packel
writeBoolean(boolean b, Parcel out) writes boolean type data to parcel
writeHashMap(Map
The source code can be found in ParcelUtils.java, and more methods and detailed parameter introduction can be found in the ParcelUtils Api Guide.
11. RandomUtils
Random number tool class, which can be used to obtain random numbers within fixed size and fixed characters, such as:
getRandom(char[] sourceChar, int length) generates a random string, all characters are within a certain string
getRandomNumbers(int length) generates random numbers
The source code can be found in RandomUtils.java, and more methods and more detailed parameter introductions can be found in the RandomUtils Api Guide.
12. ArrayUtils
Array tool class, which can be used for common operations on arrays, such as:
isEmpty(V[] sourceArray) determines whether the array is empty or has a length of 0
getLast(V[] sourceArray, V value, V defaultValue, boolean isCircle) Gets the element before an element in the array, isCircle indicates whether to loop
getNext(V[] sourceArray, V value, V defaultValue, boolean isCircle) Gets the next element of an element in the array, isCircle indicates whether to loop
The source code can be found in ArrayUtils.java, and more methods and more detailed parameter introductions can be found in the ArrayUtils Api Guide.
13. ImageUtils
Picture tool class, which can be used for conversion between Bitmap, byte array, Drawable and picture scaling. The function is currently weak and will be enhanced later. Such as:
bitmapToDrawable(Bitmap b) convert bimap to drawable
drawableToBitmap(Drawable d) drawable is converted to bitmap
drawableToByte(Drawable d) drawable is converted to byte
scaleImage(Bitmap org, float scaleWidth, float scaleHeight) scale image
The source code can be found in ImageUtils.java, and more methods and more detailed parameter introductions can be found in the ImageUtils Api Guide.
14. ListUtils
List tool class, which can be used for common List operations, such as:
isEmpty(List
join(List
addDistinctEntry(List
The source code can be found in ListUtils.java, and more methods and more detailed parameter introductions can be found in the ListUtils Api Guide.
15. MapUtils
Map tool class, which can be used for common Map operations, such as:
isEmpty(Map
parseKeyAndValueToMap(String source, String keyAndValueSeparator, String keyAndValuePairSeparator, boolean ignoreSpace) The string is parsed into map
toJson(Map
The source code can be found in MapUtils.java, and more methods and more detailed parameter introductions can be found in the MapUtils Api Guide.
16. ObjectUtils
Object tool class, which can be used for common operations on Object, such as:
isEquals(Object actual, Object expected) Compares two objects for equality
compare(V v1, V v2) compares the size of two objects
transformIntArray(int[] source) Integer array is converted to int array
The source code can be found in ObjectUtils.java, and more methods and more detailed parameter introductions can be found in the ObjectUtils Api Guide.
17. SerializeUtils
Serialization tool class, which can be used to serialize objects to files or deserialize objects from files, such as:
deserialization(String filePath) Deserializes an object from a file
serialization(String filePath, Object obj) serializes objects to files
The source code can be found in SerializeUtils.java, and more methods and detailed parameter introduction can be found in the SerializeUtils Api Guide.
18. SystemUtils
The system information tool class can be used to get the appropriate size of the thread pool. The function is currently weak and will be enhanced later. Such as:
getDefaultThreadPoolSize() gets the thread pool size that matches the system configuration
The source code can be found in SystemUtils.java, and more methods and more detailed parameter introductions can be found in the SystemUtils Api Guide.
19. TimeUtils
Time tool class, can be used for time-related operations, such as:
getCurrentTimeInLong() gets the current time
getTime(long timeInMillis, SimpleDateFormat dateFormat) Convert long to fixed format time string
The source code can be found in TimeUtils.java, and more methods and more detailed parameter introductions can be found in the TimeUtils Api Guide.