Summary of 19 common Android tools
This section mainly describes the commonly used tool classes in Android development, most of which are also applicable to Java.
Currently, these include delimiter, delimiter, ShellUtils, delimiter, FileUtils, ResourceUtils, StringUtils, ParcelUtils, delimiter, ArrayUtils, ImageUtils, delimiter, MapUtils, ObjectUtils, delimiter, SystemUtils, and timeuti.
The English version of this article see: Android Common Utils
All the code is in TrineaAndroidCommon @ Github. Welcome to Star or Fork ^ _ *. In addition to these tools, this project also includes cache and drop-down ListView. For more information, see the TrineaAndroidCommon API Guide.
Specific usage: You can directly introduce TrineaAndroidCommon as the library of your project (how to pull code and add public libraries), or extract some of them by yourself.
1. HttpUtils
The Http network tool class mainly includes httpGet, httpPost, and http parameter-related methods. Take httpGet as an example:
Static HttpResponse httpGet (HttpRequest request)
Static HttpResponse httpGet (java. lang. String httpUrl)
Static String httpGetString (String httpUrl)
The preceding three methods are used. gzip compression is used by default and bufferedReader is used to increase the read speed.
You can set url, timeout, userAgent, and other http parameters in HttpRequest.
The returned content, http response code, and http expiration time (max-age and expires of Cache-Control) can be obtained in HttpResponse.
The first two methods can be used to set advanced parameters and return rich content. The third method can be used to pass in a url to obtain the returned content. httpPost is similar. For more detailed settings, you can directly use HttpURLConnection or HttpClient of apache.
For more information about the source code, see HttpUtils. java. For more information about the methods and parameters, see HttpUtils Api Guide.
2. DownloadManagerPro
DownloadManager Enhancement Method for Android download management, which can be used to obtain download-related information, such:
GetStatusById (long) to get the Download Status
GetDownloadBytes (long) obtains the download progress.
GetBytesAndStatus (long) to get the download progress and status
GetFileName (long)
GetUri (long) to get the downloaded uri
GetReason (long) causes download failure or suspension
GetPausedReason (long) causes the download to be paused
GetErrorCode (long) gets the download error code.
The source code can be seen in DownloadManagerPro. java. For more information about methods and parameters, see DownloadManagerPro Api Guide. For Android DownManager, use the visible DownManager Demo.
3. ShellUtils
Android Shell tool class, which can be used to check system root permissions and run shell commands under shell or root users. For example:
CheckRootPermission () Check root permissions
ExecCommand (String [] commands, boolean isRoot, boolean isNeedResultMsg) shell environment to execute the command, the second parameter indicates whether the root permission to execute
ExecCommand (String command, boolean isRoot) shell environment command Execution
The source code can be seen in ShellUtils. java. For more methods and more details about parameters, see the ShellUtils Api Guide. For Silent Installation, see apk-root permission for Silent Installation.
4. PackageUtils
Android package-related tools can be used to (root) install an application, (root) uninstall the application, and determine whether the application is a system application, such:
Install (Context, String) to install the application. If the application is a system application or has been root, it is installed silently. Otherwise, it is generally installed.
Uninstall (Context, String) uninstalls an application. If it is a system application or has been root, it is uninstalled silently. Otherwise, it is generally uninstalled.
IsSystemApplication (Context, String) determines whether the application is a system application
For the source code, see PackageUtils. java. For more information about methods and parameters, see the ShellUtils Api Guide. For Silent Installation, see apk-root permission for Silent Installation.
5. PreferencesUtils
Android SharedPreferences tool class, which can be used to easily read and write data of related types to SharedPreferences, such:
PutString (Context, String, String) Stores string-type data
PutInt (Context, String, int) saves int type data
GetString (Context, String)
GetInt (Context, String)
You can modify the preference name by modifying the PREFERENCE_NAME variable.
For the source code, see PreferencesUtils. java. For more information about methods and parameters, see the PreferencesUtils Api Guide.
6. JSONUtils
JSONUtils tool class, which can be used to easily read and write data of relevant types to Json, such:
String getString (JSONObject jsonObject, String key, String defaultValue)
String getString (String jsonData, String key, String defaultValue)
Reads the value of a String key from json.
GetMap (JSONObject jsonObject, String key)
GetMap (String jsonData, String key)
Reads the value of a Map key from json.
For the source code, see JSONUtils. java. For more information about methods and parameters, see JSONUtils Api Guide.
7. FileUtils
File tool class, which can be used to read and write files and perform operations on files. For example:
ReadFile (String filePath)
WriteFile (String filePath, String content, boolean append) writes a file
GetFileSize (String path) to get the file size
DeleteFile (String path): delete an object
For more information about the source code, see FileUtils. java. For more information about the methods and parameters, see 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:
GeFileFromAssets (Context context, String fileName) to get the content of a file in the assets Directory.
GeFileFromRaw (Context context, int resId) obtains the content of a file in the raw directory.
The source code can be seen in ResourceUtils. java. For more methods and detailed parameters, see ResourceUtils Api Guide.
9. StringUtils
String tool class, which can be used for common String operations, such:
IsEmpty (String str) determines whether the String is null or the length is 0
IsBlank (String str) determines whether the String is null, whether the length is 0, or whether it is composed of spaces.
Utf8Encode (String str) is UTF-8 encoded
CapitalizeFirstLetter (String str) is capitalized.
For more information about the source code, see StringUtils. java. For more information about the methods and parameters, see StringUtils Api Guide.
10. ParcelUtils
Android Parcel tool class, which can be used to read or write special types of data from parcel, for example:
ReadBoolean (Parcel in) reads boolean data from pacel
ReadHashMap (Parcel in, ClassLoader loader) reads map-type data from pacel
WriteBoolean (boolean B, Parcel out) writes boolean data to parcel.
WriteHashMap (Map <K, V> map, Parcel out, int flags) writes map data to parcel.
For the source code, see ParcelUtils. java. For more information about methods and parameters, see ParcelUtils Api Guide.
11. RandomUtils
The random number tool can be used to obtain random numbers within fixed characters, such:
GetRandom (char [] sourceChar, int length) generates a random string, all characters in a string
GetRandomNumbers (int length) generates random numbers
The source code can be seen in RandomUtils. java. For more information about methods and parameters, see the RandomUtils Api Guide.
12. ArrayUtils
Array tool class, which can be used for common array operations, such:
IsEmpty (V [] sourceArray) determines whether the array is null or the length is 0
GetLast (V [] sourceArray, V value, V defaultValue, boolean isCircle) obtains the first element of an array. isCircle indicates whether to loop.
GetNext (V [] sourceArray, V value, V defaultValue, boolean isCircle) to obtain the next element of an array. isCircle indicates whether to loop.
For more information about the source code, see ArrayUtils. java. For more information about the methods and parameters, see ArrayUtils Api Guide.
13. ImageUtils
The image tool class can be used for Bitmap, byte array, and Drawable conversion and image scaling. Currently, the function is weak and will be enhanced later. For example:
BitmapToDrawable (Bitmap B) bimap to drawable
DrawableToBitmap (Drawable d) drawable to bitmap
DrawableToByte (Drawable d) drawable to byte
ScaleImage (Bitmap org, float scaleWidth, float scaleHeight) Scaling Image
For more information about the source code, see ImageUtils. java. For more information about the methods and parameters, see ImageUtils Api Guide.
14. ListUtils
List tool class, which can be used for List common operations, such:
IsEmpty (List <V> sourceList)
Join (List <String> list, String separator) List is converted to a String and separated by a fixed separator.
AddDistinctEntry (List <V> sourceList, V entry) add non-repeating elements to the list
For the source code, see ListUtils. java. For more methods and detailed parameter descriptions, see ListUtils Api Guide.
15. MapUtils
Map tool class, which can be used for common Map operations, such:
IsEmpty (Map <K, V> sourceMap) determines whether the map is empty or the length is 0
ParseKeyAndValueToMap (String source, String keyAndValueSeparator, String keyAndValuePairSeparator, boolean ignoreSpace) String resolved to map
ToJson (Map <String, String> map) map to json format
For more information about the source code, see MapUtils. java. For more information about the methods and parameters, see MapUtils Api Guide.
16. ObjectUtils
Object tool class, which can be used for Common Object operations, such:
IsEquals (Object actual, Object expected) compares whether two objects are equal
Compare (V v1, V v2) compares the sizes of two objects
TransformIntArray (int [] source) Integer array to int Array
The source code can be seen in ObjectUtils. java. For more information about methods and parameters, see the ObjectUtils Api Guide.
17. SerializeUtils
Serialization tool class, which can be used to serialize an object to a file or deserialize an object from a file, such:
Deserialization (String filePath) deserializes objects from a file
Serialization (String filePath, Object obj) serializes an Object to a file
For the source code, see SerializeUtils. java. For more information about methods and parameters, see SerializeUtils Api Guide.
18. SystemUtils
The system information tool class can be used to obtain the proper size of the thread pool. Currently, the function is weak and will be enhanced later. For example:
Getdefathreadthreadpoolsize () obtains the size of the thread pool consistent with the system configuration.
For more information about the source code, see SystemUtils. java. For more information about the methods and parameters, see SystemUtils Api Guide.
19. TimeUtils
Time tool class, which can be used for time-related operations, such:
GetCurrentTimeInLong () returns the current time.
GetTime (long timeInMillis, SimpleDateFormat dateFormat) converts long to a fixed format time string
For more information about the source code, see TimeUtils. java. For more information about the methods and parameters, see the TimeUtils Api Guide.