Example details Android rapid development tool class summary

  • 2021-01-19 22:25:57
  • OfStack

1. Log tool class Log.java


public class L 
{ 
private L() 
{ 
/*  Cannot be instantiated  */ 
throw new UnsupportedOperationException("Cannot be instantiated!"); 
} 
//  Do you need to print it? bug , can be in application the onCreate The function is initialized  
public static boolean isDebug = true; 
private static final String TAG = "DefaultTag"; 
//  The following 4 A is the default tag The function of  
public static void i(String msg) 
{ 
if (isDebug) 
Log.i(TAG, msg); 
} 
public static void d(String msg) 
{ 
if (isDebug) 
Log.d(TAG, msg); 
} 
public static void e(String msg) 
{ 
if (isDebug) 
Log.e(TAG, msg); 
} 
public static void v(String msg) 
{ 
if (isDebug) 
Log.v(TAG, msg); 
} 
//  The following is the incoming customizations tag The function of  
public static void i(String tag, String msg) 
{ 
if (isDebug) 
Log.i(tag, msg); 
} 
public static void d(String tag, String msg) 
{ 
if (isDebug) 
Log.i(tag, msg); 
} 
public static void e(String tag, String msg) 
{ 
if (isDebug) 
Log.i(tag, msg); 
} 
public static void v(String tag, String msg) 
{ 
if (isDebug) 
Log.i(tag, msg); 
} 
}

2. Toast Series 1 Management Tost.java


public class T 
{ 
private T() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
public static boolean isShow = true; 
/** 
*  Short time display Toast 
*/ 
public static void showShort(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Short time display Toast 
* @param message  The string resource to display id
*/ 
public static void showShort(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, CharSequence message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, int message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
}

3. SharedPreferences encapsulates classes SPUtils.java and PreferencesUtils.java

1. SPUtils.java


public class SPUtils 
{ 
/** 
*  File name saved in the phone  
*/ 
public static final String FILE_NAME = "share_data"; 
/** 
*  The method to save the data, we need to get the specific type of the saved data, and then call different save methods according to the type  
* 
* @param context 
* @param key 
* @param object 
*/ 
public static void put(Context context, String key, Object object) 
{ 
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 
Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sp.edit(); 
if (object instanceof String) 
{ 
editor.putString(key, (String) object); 
} else if (object instanceof Integer) 
{ 
editor.putInt(key, (Integer) object); 
} else if (object instanceof Boolean) 
{ 
editor.putBoolean(key, (Boolean) object); 
} else if (object instanceof Float) 
{ 
editor.putFloat(key, (Float) object); 
} else if (object instanceof Long) 
{ 
editor.putLong(key, (Long) object); 
} else 
{ 
editor.putString(key, object.toString()); 
} 
SharedPreferencesCompat.apply(editor); 
} 
/** 
*  To get the method to save the data, we find the specific type of the saved data according to the default value, and then call the method relative to that to get the value  
* 
* @param context 
* @param key 
* @param defaultObject 
* @return 
*/ 
public static Object get(Context context, String key, Object defaultObject) 
{ 
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 
Context.MODE_PRIVATE); 
if (defaultObject instanceof String) 
{ 
return sp.getString(key, (String) defaultObject); 
} else if (defaultObject instanceof Integer) 
{ 
return sp.getInt(key, (Integer) defaultObject); 
} else if (defaultObject instanceof Boolean) 
{ 
return sp.getBoolean(key, (Boolean) defaultObject); 
} else if (defaultObject instanceof Float) 
{ 
return sp.getFloat(key, (Float) defaultObject); 
} else if (defaultObject instanceof Long) 
{ 
return sp.getLong(key, (Long) defaultObject); 
} 
return null; 
} 
/** 
*  To remove a key Value already corresponds to the value  
* @param context 
* @param key 
*/ 
public static void remove(Context context, String key) 
{ 
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 
Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sp.edit(); 
editor.remove(key); 
SharedPreferencesCompat.apply(editor); 
} 
/** 
*  Clear all data  
* @param context 
*/ 
public static void clear(Context context) 
{ 
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 
Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sp.edit(); 
editor.clear(); 
SharedPreferencesCompat.apply(editor); 
} 
/** 
*  Query a key Whether it already exists  
* @param context 
* @param key 
* @return 
*/ 
public static boolean contains(Context context, String key) 
{ 
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 
Context.MODE_PRIVATE); 
return sp.contains(key); 
} 
/** 
*  Returns all key-value pairs  
* 
* @param context 
* @return 
*/ 
public static Map<String, ?> getAll(Context context) 
{ 
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, 
Context.MODE_PRIVATE); 
return sp.getAll(); 
} 
/** 
*  create 1 A solution to SharedPreferencesCompat.apply methods 1 A compatible class  
* 
* @author zhy 
* 
*/ 
private static class SharedPreferencesCompat 
{ 
private static final Method sApplyMethod = findApplyMethod(); 
/** 
*  Reflection to find apply The method of  
* 
* @return 
*/ 
@SuppressWarnings({ "unchecked", "rawtypes" }) 
private static Method findApplyMethod() 
{ 
try 
{ 
Class clz = SharedPreferences.Editor.class; 
return clz.getMethod("apply"); 
} catch (NoSuchMethodException e) 
{ 
} 
return null; 
} 
/** 
*  If found, use apply Execute, or use commit 
* 
* @param editor 
*/ 
public static void apply(SharedPreferences.Editor editor) 
{ 
try 
{ 
if (sApplyMethod != null) 
{ 
sApplyMethod.invoke(editor); 
return; 
} 
} catch (IllegalArgumentException e) 
{ 
} catch (IllegalAccessException e) 
{ 
} catch (InvocationTargetException e) 
{ 
} 
editor.commit(); 
} 
} 
}

The use of SharedPreference made recommendations for encapsulation, published put, get, remove, clear and other methods;

Note 1 that all commit operations are replaced by SharedPreferencesCompat.apply is used to replace commit as much as possible.
First, why? Because commit methods are synchronous, and most of our commit operations are in the UI thread, after all, IO operations, as asynchronous as possible;
So we use apply instead, apply writes asynchronously;
But apply is equivalent to commit new API, in order to better compatibility, we do adaptation;
SharedPreferencesCompat can also give you to create compatible classes to provide a fixed reference

2. SPUtils.java


public class PreferencesUtils {
public static String PREFERENCE_NAME = "TrineaAndroidCommon";
private PreferencesUtils() {
throw new AssertionError();
}
/**
* put string preferences
* 
* @param context
* @param key The name of the preference to modify
* @param value The new value for the preference
* @return True if the new values were successfully written to persistent storage.
*/
public static boolean putString(Context context, String key, String value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
return editor.commit();
}
/**
* get string preferences
* 
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this
* name that is not a string
* @see #getString(Context, String, String)
*/
public static String getString(Context context, String key) {
return getString(context, key, null);
}
/**
* get string preferences
* 
* @param context
* @param key The name of the preference to retrieve
* @param defaultValue Value to return if this preference does not exist
* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a string
*/
public static String getString(Context context, String key, String defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getString(key, defaultValue);
}
/**
* put int preferences
* 
* @param context
* @param key The name of the preference to modify
* @param value The new value for the preference
* @return True if the new values were successfully written to persistent storage.
*/
public static boolean putInt(Context context, String key, int value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(key, value);
return editor.commit();
}
/**
* get int preferences
* 
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or -. Throws ClassCastException if there is a preference with this
* name that is not a int
* @see #getInt(Context, String, int)
*/
public static int getInt(Context context, String key) {
return getInt(context, key, -);
}
/**
* get int preferences
* 
* @param context
* @param key The name of the preference to retrieve
* @param defaultValue Value to return if this preference does not exist
* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a int
*/
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getInt(key, defaultValue);
}
/**
* put long preferences
* 
* @param context
* @param key The name of the preference to modify
* @param value The new value for the preference
* @return True if the new values were successfully written to persistent storage.
*/
public static boolean putLong(Context context, String key, long value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putLong(key, value);
return editor.commit();
}
/**
* get long preferences
* 
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or -. Throws ClassCastException if there is a preference with this
* name that is not a long
* @see #getLong(Context, String, long)
*/
public static long getLong(Context context, String key) {
return getLong(context, key, -);
}
/**
* get long preferences
* 
* @param context
* @param key The name of the preference to retrieve
* @param defaultValue Value to return if this preference does not exist
* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a long
*/
public static long getLong(Context context, String key, long defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getLong(key, defaultValue);
}
/**
* put float preferences
* 
* @param context
* @param key The name of the preference to modify
* @param value The new value for the preference
* @return True if the new values were successfully written to persistent storage.
*/
public static boolean putFloat(Context context, String key, float value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putFloat(key, value);
return editor.commit();
}
/**
* get float preferences
* 
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or -. Throws ClassCastException if there is a preference with this
* name that is not a float
* @see #getFloat(Context, String, float)
*/
public static float getFloat(Context context, String key) {
return getFloat(context, key, -);
}
/**
* get float preferences
* 
* @param context
* @param key The name of the preference to retrieve
* @param defaultValue Value to return if this preference does not exist
* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a float
*/
public static float getFloat(Context context, String key, float defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getFloat(key, defaultValue);
}
/**
* put boolean preferences
* 
* @param context
* @param key The name of the preference to modify
* @param value The new value for the preference
* @return True if the new values were successfully written to persistent storage.
*/
public static boolean putBoolean(Context context, String key, boolean value) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
return editor.commit();
}
/**
* get boolean preferences, default is false
* 
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this
* name that is not a boolean
* @see #getBoolean(Context, String, boolean)
*/
public static boolean getBoolean(Context context, String key) {
return getBoolean(context, key, false);
}
/**
* get boolean preferences
* 
* @param context
* @param key The name of the preference to retrieve
* @param defaultValue Value to return if this preference does not exist
* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
* this name that is not a boolean
*/
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
return settings.getBoolean(key, defaultValue);
}
}

4. Unit Conversion Class DensityUtils.java


public class DensityUtils 
{ 
private DensityUtils() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
/** 
* dp turn px 
* 
* @param context 
* @param val 
* @return 
*/ 
public static int dppx(Context context, float dpVal) 
{ 
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
dpVal, context.getResources().getDisplayMetrics()); 
} 
/** 
* sp turn px 
* 
* @param context 
* @param val 
* @return 
*/ 
public static int sppx(Context context, float spVal) 
{ 
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 
spVal, context.getResources().getDisplayMetrics()); 
} 
/** 
* px turn dp 
* 
* @param context 
* @param pxVal 
* @return 
*/ 
public static float pxdp(Context context, float pxVal) 
{ 
final float scale = context.getResources().getDisplayMetrics().density; 
return (pxVal / scale); 
} 
/** 
* px turn sp 
* 
* @param fontScale 
* @param pxVal 
* @return 
*/ 
public static float pxsp(Context context, float pxVal) 
{ 
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity); 
} 
}

TypedValue:

Container for a dynamically typed data value. Primarily used with Resources for holding resource values.
applyDimension(int unit, float value, DisplayMetrics metrics) :
Converts an unpacked complex data value holding a dimension to its final floating point value.

5. SD card related auxiliary class SDCardUtils.java


public class SDCardUtils 
{ 
private SDCardUtils() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
/** 
*  judge SDCard Whether the available  
* 
* @return 
*/ 
public static boolean isSDCardEnable() 
{ 
return Environment.getExternalStorageState().equals( 
Environment.MEDIA_MOUNTED); 
} 
/** 
*  To obtain SD The card path  
* 
* @return 
*/ 
public static String getSDCardPath() 
{ 
return Environment.getExternalStorageDirectory().getAbsolutePath() 
+ File.separator; 
} 
/** 
*  To obtain SD The remaining capacity of the card   unit byte 
* 
* @return 
*/ 
public static long getSDCardAllSize() 
{ 
if (isSDCardEnable()) 
{ 
StatFs stat = new StatFs(getSDCardPath()); 
//  Gets the number of free data blocks  
long availableBlocks = (long) stat.getAvailableBlocks() - ; 
//  Gets the size of a single data block ( byte )  
long freeBlocks = stat.getAvailableBlocks(); 
return freeBlocks * availableBlocks; 
} 
return ; 
} 
/** 
*  Gets the number of bytes of remaining available capacity in the space where the specified path resides byte 
* 
* @param filePath 
* @return  Capacity of bytes  SDCard Available space, internal storage of available space  
*/ 
public static long getFreeBytes(String filePath) 
{ 
//  If it is sd The path under the card is obtained sd Card available capacity  
if (filePath.startsWith(getSDCardPath())) 
{ 
filePath = getSDCardPath(); 
} else 
{//  If it is the path to the internal storage, the available capacity of the memory store is obtained  
filePath = Environment.getDataDirectory().getAbsolutePath(); 
} 
StatFs stat = new StatFs(filePath); 
long availableBlocks = (long) stat.getAvailableBlocks() - ; 
return stat.getBlockSize() * availableBlocks; 
} 
/** 
*  Gets the system storage path  
* 
* @return 
*/ 
public static String getRootDirectoryPath() 
{ 
return Environment.getRootDirectory().getAbsolutePath(); 
} 
}

StatFs is a class provided by Android:

Retrieve overall information about the space on a filesystem. This is a wrapper for Unix statvfs().

Retrieves the entire information space of a file system. This is an Unix statvfs() wrapper

6. Screen related auxiliary class ScreenUtils.java


public class ScreenUtils 
{ 
private ScreenUtils() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
/** 
*  Get the screen height  
* 
* @param context 
* @return 
*/ 
public static int getScreenWidth(Context context) 
{ 
WindowManager wm = (WindowManager) context 
.getSystemService(Context.WINDOW_SERVICE); 
DisplayMetrics outMetrics = new DisplayMetrics(); 
wm.getDefaultDisplay().getMetrics(outMetrics); 
return outMetrics.widthPixels; 
} 
/** 
*  Obtain screen width  
* 
* @param context 
* @return 
*/ 
public static int getScreenHeight(Context context) 
{ 
WindowManager wm = (WindowManager) context 
.getSystemService(Context.WINDOW_SERVICE); 
DisplayMetrics outMetrics = new DisplayMetrics(); 
wm.getDefaultDisplay().getMetrics(outMetrics); 
return outMetrics.heightPixels; 
} 
/** 
*  Gets the height of the status bar  
* 
* @param context 
* @return 
*/ 
public static int getStatusHeight(Context context) 
{ 
int statusHeight = -; 
try 
{ 
Class<?> clazz = Class.forName("com.android.internal.R$dimen"); 
Object object = clazz.newInstance(); 
int height = Integer.parseInt(clazz.getField("status_bar_height") 
.get(object).toString()); 
statusHeight = context.getResources().getDimensionPixelSize(height); 
} catch (Exception e) 
{ 
e.printStackTrace(); 
} 
return statusHeight; 
} 
/** 
*  Gets a snapshot of the current screen, including the status bar  
* 
* @param activity 
* @return 
*/ 
public static Bitmap snapShotWithStatusBar(Activity activity) 
{ 
View view = activity.getWindow().getDecorView(); 
view.setDrawingCacheEnabled(true); 
view.buildDrawingCache(); 
Bitmap bmp = view.getDrawingCache(); 
int width = getScreenWidth(activity); 
int height = getScreenHeight(activity); 
Bitmap bp = null; 
bp = Bitmap.createBitmap(bmp, , , width, height); 
view.destroyDrawingCache(); 
return bp; 
} 
/** 
*  Gets a screenshot of the current screen without the status bar  
* 
* @param activity 
* @return 
*/ 
public static Bitmap snapShotWithoutStatusBar(Activity activity) 
{ 
View view = activity.getWindow().getDecorView(); 
view.setDrawingCacheEnabled(true); 
view.buildDrawingCache(); 
Bitmap bmp = view.getDrawingCache(); 
Rect frame = new Rect(); 
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 
int statusBarHeight = frame.top; 
int width = getScreenWidth(activity); 
int height = getScreenHeight(activity); 
Bitmap bp = null; 
bp = Bitmap.createBitmap(bmp, , statusBarHeight, width, height 
- statusBarHeight); 
view.destroyDrawingCache(); 
return bp; 
} 
}

7. App related auxiliary classes APPUtils.java


public class AppUtils 
{ 
private AppUtils() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
/** 
*  Gets the application name  
*/ 
public static String getAppName(Context context) 
{ 
try 
{ 
PackageManager packageManager = context.getPackageManager(); 
PackageInfo packageInfo = packageManager.getPackageInfo( 
context.getPackageName(), ); 
int labelRes = packageInfo.applicationInfo.labelRes; 
return context.getResources().getString(labelRes); 
} catch (NameNotFoundException e) 
{ 
e.printStackTrace(); 
} 
return null; 
} 
/** 
* [ Gets the application version name information ] 
* 
* @param context 
* @return  The version name of the current application  
*/ 
public static String getVersionName(Context context) 
{ 
try 
{ 
PackageManager packageManager = context.getPackageManager(); 
PackageInfo packageInfo = packageManager.getPackageInfo( 
context.getPackageName(), ); 
return packageInfo.versionName; 
} catch (NameNotFoundException e) 
{ 
e.printStackTrace(); 
} 
return null; 
} 
}

8. Soft keyboard related auxiliary class KeyBoardUtils.java


/** 
*  Turn on or off the soft keyboard  
*/ 
public class KeyBoardUtils 
{ 
/** 
*  Clock in the soft keyboard  
* 
* @param mEditText  Input box  
* @param mContext  context  
*/ 
public static void openKeybord(EditText mEditText, Context mContext) 
{ 
InputMethodManager imm = (InputMethodManager) mContext 
.getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); 
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 
InputMethodManager.HIDE_IMPLICIT_ONLY); 
} 
/** 
*  Close the Soft Keyboard  
* 
* @param mEditText  Input box  
* @param mContext  context  
*/ 
public static void closeKeybord(EditText mEditText, Context mContext) 
{ 
InputMethodManager imm = (InputMethodManager) mContext 
.getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), ); 
} 
}

9. Network related auxiliary classes NetUtils.java


public class NetUtils 
{ 
private NetUtils() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
/** 
*  Determine whether the network is connected  
*/ 
public static boolean isConnected(Context context) 
{ 
ConnectivityManager connectivity = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
if (null != connectivity) 
{ 
NetworkInfo info = connectivity.getActiveNetworkInfo(); 
if (null != info && info.isConnected()) 
{ 
if (info.getState() == NetworkInfo.State.CONNECTED) 
{ 
return true; 
} 
} 
} 
return false; 
} 
/** 
*  Determine whether or not wifi The connection  
*/ 
public static boolean isWifi(Context context) 
{ 
ConnectivityManager cm = (ConnectivityManager) context 
.getSystemService(Context.CONNECTIVITY_SERVICE); 
if (cm == null) 
return false; 
return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI; 
} 
/** 
*  Open the network Settings interface  
*/ 
public static void openSetting(Activity activity) 
{ 
Intent intent = new Intent("/"); 
ComponentName cm = new ComponentName("com.android.settings", 
"com.android.settings.WirelessSettings"); 
intent.setComponent(cm); 
intent.setAction("android.intent.action.VIEW"); 
activity.startActivityForResult(intent, ); 
} 
}

10. Http related auxiliary classes HttpUtils.java


public class T 
{ 
private T() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
public static boolean isShow = true; 
/** 
*  Short time display Toast 
*/ 
public static void showShort(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Short time display Toast 
* @param message  The string resource to display id
*/ 
public static void showShort(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, CharSequence message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, int message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
}
0

101. Time tool class TimeUtils.java


public class T 
{ 
private T() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
public static boolean isShow = true; 
/** 
*  Short time display Toast 
*/ 
public static void showShort(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Short time display Toast 
* @param message  The string resource to display id
*/ 
public static void showShort(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, CharSequence message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, int message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
}
1

102. File tool class FileUtils.java


public class T 
{ 
private T() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
public static boolean isShow = true; 
/** 
*  Short time display Toast 
*/ 
public static void showShort(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Short time display Toast 
* @param message  The string resource to display id
*/ 
public static void showShort(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, CharSequence message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, int message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
}
2

103. assets and raw resource tool class ResourceUtils.java


public class ResourceUtils {
private ResourceUtils() {
throw new AssertionError();
}
/**
* get an asset using ACCESS_STREAMING mode. This provides access to files that have been bundled with an
* application as assets -- that is, files placed in to the "assets" directory.
* 
* @param context
* @param fileName The name of the asset to open. This name can be hierarchical.
* @return
*/
public static String geFileFromAssets(Context context, String fileName) {
if (context == null || StringUtils.isEmpty(fileName)) {
return null;
}
StringBuilder s = new StringBuilder("");
try {
InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
s.append(line);
}
return s.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* get content from a raw resource. This can only be used with resources whose value is the name of an asset files
* -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color
* resources.
* 
* @param context
* @param resId The resource identifier to open, as generated by the appt tool.
* @return
*/
public static String geFileFromRaw(Context context, int resId) {
if (context == null) {
return null;
}
StringBuilder s = new StringBuilder();
try {
InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
s.append(line);
}
return s.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* same to {@link ResourceUtils#geFileFromAssets(Context, String)}, but return type is List<String>
* 
* @param context
* @param fileName
* @return
*/
public static List<String> geFileToListFromAssets(Context context, String fileName) {
if (context == null || StringUtils.isEmpty(fileName)) {
return null;
}
List<String> fileContent = new ArrayList<String>();
try {
InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
fileContent.add(line);
}
br.close();
return fileContent;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* same to {@link ResourceUtils#geFileFromRaw(Context, int)}, but return type is List<String>
* 
* @param context
* @param resId
* @return
*/
public static List<String> geFileToListFromRaw(Context context, int resId) {
if (context == null) {
return null;
}
List<String> fileContent = new ArrayList<String>();
BufferedReader reader = null;
try {
InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
reader = new BufferedReader(in);
String line = null;
while ((line = reader.readLine()) != null) {
fileContent.add(line);
}
reader.close();
return fileContent;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}

104. Singleton tool class SingletonUtils.java


public class T 
{ 
private T() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
public static boolean isShow = true; 
/** 
*  Short time display Toast 
*/ 
public static void showShort(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Short time display Toast 
* @param message  The string resource to display id
*/ 
public static void showShort(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, CharSequence message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, int message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
}
4

105. Database utility class SqliteUtils.java


public class T 
{ 
private T() 
{ 
/* cannot be instantiated */ 
throw new UnsupportedOperationException("cannot be instantiated"); 
} 
public static boolean isShow = true; 
/** 
*  Short time display Toast 
*/ 
public static void showShort(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Short time display Toast 
* @param message  The string resource to display id
*/ 
public static void showShort(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, CharSequence message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Long time display Toast 
*/ 
public static void showLong(Context context, int message) 
{ 
if (isShow) 
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, CharSequence message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
/** 
*  Custom display Toast time  
*/ 
public static void show(Context context, int message, int duration) 
{ 
if (isShow) 
Toast.makeText(context, message, duration).show(); 
} 
}
5

Related articles: