{"id":8170,"date":"2024-06-20T13:01:01","date_gmt":"2024-06-20T05:01:01","guid":{"rendered":""},"modified":"2024-06-20T13:01:01","modified_gmt":"2024-06-20T05:01:01","slug":"android app\u5feb\u901f\u5f00\u53d1_android app\u5f00\u53d1\u5de5\u5177","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8170.html","title":{"rendered":"android app\u5feb\u901f\u5f00\u53d1_android app\u5f00\u53d1\u5de5\u5177"},"content":{"rendered":"
\n

\u4e00\u3001\u65e5\u5fd7\u5de5\u5177\u7c7b Log.java<\/p>\n

public class L<\/p>\n

{
\n <\/p>\n

private L()<\/p>\n

{
\n <\/p>\n

\/* \u4e0d\u53ef\u88ab\u5b9e\u4f8b\u5316 *\/<\/p>\n

throw new UnsupportedOperationException(\"Cannot be instantiated!\");<\/p>\n

}<\/p>\n

\/\/ \u662f\u5426\u9700\u8981\u6253\u5370bug\uff0c\u53ef\u4ee5\u5728application\u7684onCreate\u51fd\u6570\u91cc\u9762\u521d\u59cb\u5316<\/p>\n

public static boolean isDebug = true;<\/p>\n

private static final String TAG = \"DefaultTag\";<\/p>\n

\/\/ \u4e0b\u9762\u56db\u4e2a\u662f\u9ed8\u8ba4tag\u7684\u51fd\u6570<\/p>\n

public static void i(String msg)<\/p>\n

{
\n <\/p>\n

if (isDebug)<\/p>\n

Log.i(TAG, msg);<\/p>\n

}<\/p>\n

public static void d(String msg)<\/p>\n

{
\n <\/p>\n

if (isDebug)<\/p>\n

Log.d(TAG, msg);<\/p>\n

}<\/p>\n

public static void e(String msg)<\/p>\n

{
\n <\/p>\n

if (isDebug)<\/p>\n

Log.e(TAG, msg);<\/p>\n

}<\/p>\n

public static void v(String msg)<\/p>\n

{
\n <\/p>\n

if (isDebug)<\/p>\n

Log.v(TAG, msg);<\/p>\n

}<\/p>\n

\/\/ \u4e0b\u9762\u662f\u4f20\u5165\u81ea\u5b9a\u4e49tag\u7684\u51fd\u6570<\/p>\n

public static void i(String tag, String msg)<\/p>\n

{
\n <\/p>\n

if (isDebug)<\/p>\n

Log.i(tag, msg);<\/p>\n

}<\/p>\n

public static void d(String tag, String msg)<\/p>\n

{
\n <\/p>\n

if (isDebug)<\/p>\n

Log.i(tag, msg);<\/p>\n

}<\/p>\n

public static void e(String tag, String msg)<\/p>\n

{
\n <\/p>\n

if (isDebug)<\/p>\n

Log.i(tag, msg);<\/p>\n

}<\/p>\n

public static void v(String tag, String msg)<\/p>\n

{
\n <\/p>\n

if (isDebug)<\/p>\n

Log.i(tag, msg);<\/p>\n

}<\/p>\n

}<\/p>\n

\u4e8c\u3001Toast\u7edf\u4e00\u7ba1\u7406\u7c7b Tost.java<\/p>\n

public class T<\/p>\n

{
\n <\/p>\n

private T()<\/p>\n

{
\n <\/p>\n

\/* cannot be instantiated *\/<\/p>\n

throw new UnsupportedOperationException(\"cannot be instantiated\");<\/p>\n

}<\/p>\n

public static boolean isShow = true;<\/p>\n

\/**<\/p>\n

* \u77ed\u65f6\u95f4\u663e\u793aToast<\/p>\n

*\/<\/p>\n

public static void showShort(Context context, CharSequence message)<\/p>\n

{
\n <\/p>\n

if (isShow)<\/p>\n

Toast.makeText(context, message, Toast.LENGTH_SHORT).show();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u77ed\u65f6\u95f4\u663e\u793aToast<\/p>\n

* @param message \u8981\u663e\u793a\u7684\u5b57\u7b26\u4e32\u8d44\u6e90\u7684id<\/p>\n

*\/<\/p>\n

public static void showShort(Context context, int message)<\/p>\n

{
\n <\/p>\n

if (isShow)<\/p>\n

Toast.makeText(context, message, Toast.LENGTH_SHORT).show();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u957f\u65f6\u95f4\u663e\u793aToast<\/p>\n

*\/<\/p>\n

public static void showLong(Context context, CharSequence message)<\/p>\n

{
\n <\/p>\n

if (isShow)<\/p>\n

Toast.makeText(context, message, Toast.LENGTH_LONG).show();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u957f\u65f6\u95f4\u663e\u793aToast<\/p>\n

*\/<\/p>\n

public static void showLong(Context context, int message)<\/p>\n

{
\n <\/p>\n

if (isShow)<\/p>\n

Toast.makeText(context, message, Toast.LENGTH_LONG).show();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u81ea\u5b9a\u4e49\u663e\u793aToast\u65f6\u95f4<\/p>\n

*\/<\/p>\n

public static void show(Context context, CharSequence message, int duration)<\/p>\n

{
\n <\/p>\n

if (isShow)<\/p>\n

Toast.makeText(context, message, duration).show();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u81ea\u5b9a\u4e49\u663e\u793aToast\u65f6\u95f4<\/p>\n

*\/<\/p>\n

public static void show(Context context, int message, int duration)<\/p>\n

{
\n <\/p>\n

if (isShow)<\/p>\n

Toast.makeText(context, message, duration).show();<\/p>\n

}<\/p>\n

}<\/p>\n

\u4e09\u3001SharedPreferences\u5c01\u88c5\u7c7b SPUtils.java \u548c PreferencesUtils.java<\/p>\n

1. SPUtils.java<\/p>\n

public class SPUtils<\/p>\n

{
\n <\/p>\n

\/**<\/p>\n

* \u4fdd\u5b58\u5728\u624b\u673a\u91cc\u9762\u7684\u6587\u4ef6\u540d<\/p>\n

*\/<\/p>\n

public static final String FILE_NAME = \"share_data\";<\/p>\n

\/**<\/p>\n

* \u4fdd\u5b58\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u9700\u8981\u62ff\u5230\u4fdd\u5b58\u6570\u636e\u7684\u5177\u4f53\u7c7b\u578b\uff0c\u7136\u540e\u6839\u636e\u7c7b\u578b\u8c03\u7528\u4e0d\u540c\u7684\u4fdd\u5b58\u65b9\u6cd5<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key<\/p>\n

* @param object<\/p>\n

*\/<\/p>\n

public static void put(Context context, String key, Object object)<\/p>\n

{
\n <\/p>\n

SharedPreferences sp = context.getSharedPreferences(FILE_NAME,<\/p>\n

Context.MODE_PRIVATE);<\/p>\n

SharedPreferences.Editor editor = sp.edit();<\/p>\n

if (object instanceof String)<\/p>\n

{
\n <\/p>\n

editor.putString(key, (String) object);<\/p>\n

} else if (object instanceof Integer)<\/p>\n

{
\n <\/p>\n

editor.putInt(key, (Integer) object);<\/p>\n

} else if (object instanceof Boolean)<\/p>\n

{
\n <\/p>\n

editor.putBoolean(key, (Boolean) object);<\/p>\n

} else if (object instanceof Float)<\/p>\n

{
\n <\/p>\n

editor.putFloat(key, (Float) object);<\/p>\n

} else if (object instanceof Long)<\/p>\n

{
\n <\/p>\n

editor.putLong(key, (Long) object);<\/p>\n

} else<\/p>\n

{
\n <\/p>\n

editor.putString(key, object.toString());<\/p>\n

}<\/p>\n

SharedPreferencesCompat.apply(editor);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u5f97\u5230\u4fdd\u5b58\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u6839\u636e\u9ed8\u8ba4\u503c\u5f97\u5230\u4fdd\u5b58\u7684\u6570\u636e\u7684\u5177\u4f53\u7c7b\u578b\uff0c\u7136\u540e\u8c03\u7528\u76f8\u5bf9\u4e8e\u7684\u65b9\u6cd5\u83b7\u53d6\u503c<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key<\/p>\n

* @param defaultObject<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static Object get(Context context, String key, Object defaultObject)<\/p>\n

{
\n <\/p>\n

SharedPreferences sp = context.getSharedPreferences(FILE_NAME,<\/p>\n

Context.MODE_PRIVATE);<\/p>\n

if (defaultObject instanceof String)<\/p>\n

{
\n <\/p>\n

return sp.getString(key, (String) defaultObject);<\/p>\n

} else if (defaultObject instanceof Integer)<\/p>\n

{
\n <\/p>\n

return sp.getInt(key, (Integer) defaultObject);<\/p>\n

} else if (defaultObject instanceof Boolean)<\/p>\n

{
\n <\/p>\n

return sp.getBoolean(key, (Boolean) defaultObject);<\/p>\n

} else if (defaultObject instanceof Float)<\/p>\n

{
\n <\/p>\n

return sp.getFloat(key, (Float) defaultObject);<\/p>\n

} else if (defaultObject instanceof Long)<\/p>\n

{
\n <\/p>\n

return sp.getLong(key, (Long) defaultObject);<\/p>\n

}<\/p>\n

return null;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u79fb\u9664\u67d0\u4e2akey\u503c\u5df2\u7ecf\u5bf9\u5e94\u7684\u503c<\/p>\n

* @param context<\/p>\n

* @param key<\/p>\n

*\/<\/p>\n

public static void remove(Context context, String key)<\/p>\n

{
\n <\/p>\n

SharedPreferences sp = context.getSharedPreferences(FILE_NAME,<\/p>\n

Context.MODE_PRIVATE);<\/p>\n

SharedPreferences.Editor editor = sp.edit();<\/p>\n

editor.remove(key);<\/p>\n

SharedPreferencesCompat.apply(editor);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u6e05\u9664\u6240\u6709\u6570\u636e<\/p>\n

* @param context<\/p>\n

*\/<\/p>\n

public static void clear(Context context)<\/p>\n

{
\n <\/p>\n

SharedPreferences sp = context.getSharedPreferences(FILE_NAME,<\/p>\n

Context.MODE_PRIVATE);<\/p>\n

SharedPreferences.Editor editor = sp.edit();<\/p>\n

editor.clear();<\/p>\n

SharedPreferencesCompat.apply(editor);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u67e5\u8be2\u67d0\u4e2akey\u662f\u5426\u5df2\u7ecf\u5b58\u5728<\/p>\n

* @param context<\/p>\n

* @param key<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static boolean contains(Context context, String key)<\/p>\n

{
\n <\/p>\n

SharedPreferences sp = context.getSharedPreferences(FILE_NAME,<\/p>\n

Context.MODE_PRIVATE);<\/p>\n

return sp.contains(key);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u8fd4\u56de\u6240\u6709\u7684\u952e\u503c\u5bf9<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static Map getAll(Context context)<\/p>\n

{
\n <\/p>\n

SharedPreferences sp = context.getSharedPreferences(FILE_NAME,<\/p>\n

Context.MODE_PRIVATE);<\/p>\n

return sp.getAll();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u521b\u5efa\u4e00\u4e2a\u89e3\u51b3SharedPreferencesCompat.apply\u65b9\u6cd5\u7684\u4e00\u4e2a\u517c\u5bb9\u7c7b<\/p>\n

*<\/p>\n

* @author zhy<\/p>\n

*<\/p>\n

*\/<\/p>\n

private static class SharedPreferencesCompat<\/p>\n

{
\n <\/p>\n

private static final Method sApplyMethod = findApplyMethod();<\/p>\n

\/**<\/p>\n

* \u53cd\u5c04\u67e5\u627eapply\u7684\u65b9\u6cd5<\/p>\n

*<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

@SuppressWarnings({ \"unchecked\", \"rawtypes\" })<\/p>\n

private static Method findApplyMethod()<\/p>\n

{
\n <\/p>\n

try<\/p>\n

{
\n <\/p>\n

Class clz = SharedPreferences.Editor.class;<\/p>\n

return clz.getMethod(\"apply\");<\/p>\n

} catch (NoSuchMethodException e)<\/p>\n

{
\n <\/p>\n

}<\/p>\n

return null;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u5982\u679c\u627e\u5230\u5219\u4f7f\u7528apply\u6267\u884c\uff0c\u5426\u5219\u4f7f\u7528commit<\/p>\n

*<\/p>\n

* @param editor<\/p>\n

*\/<\/p>\n

public static void apply(SharedPreferences.Editor editor)<\/p>\n

{
\n <\/p>\n

try<\/p>\n

{
\n <\/p>\n

if (sApplyMethod != null)<\/p>\n

{
\n <\/p>\n

sApplyMethod.invoke(editor);<\/p>\n

return;<\/p>\n

}<\/p>\n

} catch (IllegalArgumentException e)<\/p>\n

{
\n <\/p>\n

} catch (IllegalAccessException e)<\/p>\n

{
\n <\/p>\n

} catch (InvocationTargetException e)<\/p>\n

{
\n <\/p>\n

}<\/p>\n

editor.commit();<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

\u5bf9SharedPreference\u7684\u4f7f\u7528\u505a\u4e86\u5efa\u8bae\u7684\u5c01\u88c5\uff0c\u5bf9\u5916\u516c\u5e03\u51faput\uff0cget\uff0cremove\uff0cclear\u7b49\u7b49\u65b9\u6cd5\uff1b<\/p>\n

\u6ce8\u610f\u4e00\u70b9\uff0c\u91cc\u9762\u6240\u6709\u7684commit\u64cd\u4f5c\u4f7f\u7528\u4e86SharedPreferencesCompat.apply\u8fdb\u884c\u4e86\u66ff\u4ee3\uff0c\u76ee\u7684\u662f\u5c3d\u53ef\u80fd\u7684\u4f7f\u7528apply\u4ee3\u66ffcommit.<\/p>\n

\u9996\u5148\u8bf4\u4e0b\u4e3a\u4ec0\u4e48\uff0c\u56e0\u4e3acommit\u65b9\u6cd5\u662f\u540c\u6b65\u7684\uff0c\u5e76\u4e14\u6211\u4eec\u5f88\u591a\u65f6\u5019\u7684commit\u64cd\u4f5c\u90fd\u662fUI\u7ebf\u7a0b\u4e2d\uff0c\u6bd5\u7adf\u662fIO\u64cd\u4f5c\uff0c\u5c3d\u53ef\u80fd\u5f02\u6b65\uff1b<\/p>\n

\u6240\u4ee5\u6211\u4eec\u4f7f\u7528apply\u8fdb\u884c\u66ff\u4ee3\uff0capply\u5f02\u6b65\u7684\u8fdb\u884c\u5199\u5165\uff1b<\/p>\n

\u4f46\u662fapply\u76f8\u5f53\u4e8ecommit\u6765\u8bf4\u662fnew API\u5462\uff0c\u4e3a\u4e86\u66f4\u597d\u7684\u517c\u5bb9\uff0c\u6211\u4eec\u505a\u4e86\u9002\u914d\uff1b<\/p>\n

SharedPreferencesCompat\u4e5f\u53ef\u4ee5\u7ed9\u5927\u5bb6\u521b\u5efa\u517c\u5bb9\u7c7b\u63d0\u4f9b\u4e86\u4e00\u5b9a\u7684\u53c2\u8003<\/p>\n

2. SPUtils.java<\/p>\n

public class PreferencesUtils {
\n <\/p>\n

public static String PREFERENCE_NAME = \"TrineaAndroidCommon\";<\/p>\n

private PreferencesUtils() {
\n <\/p>\n

throw new AssertionError();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* put string preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to modify<\/p>\n

* @param value The new value for the preference<\/p>\n

* @return True if the new values were successfully written to persistent storage.<\/p>\n

*\/<\/p>\n

public static boolean putString(Context context, String key, String value) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

SharedPreferences.Editor editor = settings.edit();<\/p>\n

editor.putString(key, value);<\/p>\n

return editor.commit();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get string preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this<\/p>\n

* name that is not a string<\/p>\n

* @see #getString(Context, String, String)<\/p>\n

*\/<\/p>\n

public static String getString(Context context, String key) {
\n <\/p>\n

return getString(context, key, null);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get string preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @param defaultValue Value to return if this preference does not exist<\/p>\n

* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with<\/p>\n

* this name that is not a string<\/p>\n

*\/<\/p>\n

public static String getString(Context context, String key, String defaultValue) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

return settings.getString(key, defaultValue);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* put int preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to modify<\/p>\n

* @param value The new value for the preference<\/p>\n

* @return True if the new values were successfully written to persistent storage.<\/p>\n

*\/<\/p>\n

public static boolean putInt(Context context, String key, int value) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

SharedPreferences.Editor editor = settings.edit();<\/p>\n

editor.putInt(key, value);<\/p>\n

return editor.commit();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get int preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @return The preference value if it exists, or -. Throws ClassCastException if there is a preference with this<\/p>\n

* name that is not a int<\/p>\n

* @see #getInt(Context, String, int)<\/p>\n

*\/<\/p>\n

public static int getInt(Context context, String key) {
\n <\/p>\n

return getInt(context, key, -);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get int preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @param defaultValue Value to return if this preference does not exist<\/p>\n

* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with<\/p>\n

* this name that is not a int<\/p>\n

*\/<\/p>\n

public static int getInt(Context context, String key, int defaultValue) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

return settings.getInt(key, defaultValue);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* put long preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to modify<\/p>\n

* @param value The new value for the preference<\/p>\n

* @return True if the new values were successfully written to persistent storage.<\/p>\n

*\/<\/p>\n

public static boolean putLong(Context context, String key, long value) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

SharedPreferences.Editor editor = settings.edit();<\/p>\n

editor.putLong(key, value);<\/p>\n

return editor.commit();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get long preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @return The preference value if it exists, or -. Throws ClassCastException if there is a preference with this<\/p>\n

* name that is not a long<\/p>\n

* @see #getLong(Context, String, long)<\/p>\n

*\/<\/p>\n

public static long getLong(Context context, String key) {
\n <\/p>\n

return getLong(context, key, -);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get long preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @param defaultValue Value to return if this preference does not exist<\/p>\n

* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with<\/p>\n

* this name that is not a long<\/p>\n

*\/<\/p>\n

public static long getLong(Context context, String key, long defaultValue) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

return settings.getLong(key, defaultValue);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* put float preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to modify<\/p>\n

* @param value The new value for the preference<\/p>\n

* @return True if the new values were successfully written to persistent storage.<\/p>\n

*\/<\/p>\n

public static boolean putFloat(Context context, String key, float value) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

SharedPreferences.Editor editor = settings.edit();<\/p>\n

editor.putFloat(key, value);<\/p>\n

return editor.commit();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get float preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @return The preference value if it exists, or -. Throws ClassCastException if there is a preference with this<\/p>\n

* name that is not a float<\/p>\n

* @see #getFloat(Context, String, float)<\/p>\n

*\/<\/p>\n

public static float getFloat(Context context, String key) {
\n <\/p>\n

return getFloat(context, key, -);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get float preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @param defaultValue Value to return if this preference does not exist<\/p>\n

* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with<\/p>\n

* this name that is not a float<\/p>\n

*\/<\/p>\n

public static float getFloat(Context context, String key, float defaultValue) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

return settings.getFloat(key, defaultValue);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* put boolean preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to modify<\/p>\n

* @param value The new value for the preference<\/p>\n

* @return True if the new values were successfully written to persistent storage.<\/p>\n

*\/<\/p>\n

public static boolean putBoolean(Context context, String key, boolean value) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

SharedPreferences.Editor editor = settings.edit();<\/p>\n

editor.putBoolean(key, value);<\/p>\n

return editor.commit();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get boolean preferences, default is false<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this<\/p>\n

* name that is not a boolean<\/p>\n

* @see #getBoolean(Context, String, boolean)<\/p>\n

*\/<\/p>\n

public static boolean getBoolean(Context context, String key) {
\n <\/p>\n

return getBoolean(context, key, false);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get boolean preferences<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param key The name of the preference to retrieve<\/p>\n

* @param defaultValue Value to return if this preference does not exist<\/p>\n

* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with<\/p>\n

* this name that is not a boolean<\/p>\n

*\/<\/p>\n

public static boolean getBoolean(Context context, String key, boolean defaultValue) {
\n <\/p>\n

SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);<\/p>\n

return settings.getBoolean(key, defaultValue);<\/p>\n

}<\/p>\n

}<\/p>\n

\u56db\u3001\u5355\u4f4d\u8f6c\u6362\u7c7b DensityUtils.java<\/p>\n

public class DensityUtils<\/p>\n

{
\n <\/p>\n

private DensityUtils()<\/p>\n

{
\n <\/p>\n

\/* cannot be instantiated *\/<\/p>\n

throw new UnsupportedOperationException(\"cannot be instantiated\");<\/p>\n

}<\/p>\n

\/**<\/p>\n

* dp\u8f6cpx<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param val<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static int dppx(Context context, float dpVal)<\/p>\n

{
\n <\/p>\n

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,<\/p>\n

dpVal, context.getResources().getDisplayMetrics());<\/p>\n

}<\/p>\n

\/**<\/p>\n

* sp\u8f6cpx<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param val<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static int sppx(Context context, float spVal)<\/p>\n

{
\n <\/p>\n

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,<\/p>\n

spVal, context.getResources().getDisplayMetrics());<\/p>\n

}<\/p>\n

\/**<\/p>\n

* px\u8f6cdp<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param pxVal<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static float pxdp(Context context, float pxVal)<\/p>\n

{
\n <\/p>\n

final float scale = context.getResources().getDisplayMetrics().density;<\/p>\n

return (pxVal \/ scale);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* px\u8f6csp<\/p>\n

*<\/p>\n

* @param fontScale<\/p>\n

* @param pxVal<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static float pxsp(Context context, float pxVal)<\/p>\n

{
\n <\/p>\n

return (pxVal \/ context.getResources().getDisplayMetrics().scaledDensity);<\/p>\n

}<\/p>\n

}<\/p>\n

TypedValue\uff1a<\/p>\n

Container for a dynamically typed data value. Primarily used with Resources for holding resource values.<\/p>\n

applyDimension(int unit, float value, DisplayMetrics metrics)\uff1a<\/p>\n

Converts an unpacked complex data value holding a dimension to its final floating point value.<\/p>\n

\u4e94\u3001SD\u5361\u76f8\u5173\u8f85\u52a9\u7c7b SDCardUtils.java<\/p>\n

public class SDCardUtils<\/p>\n

{
\n <\/p>\n

private SDCardUtils()<\/p>\n

{
\n <\/p>\n

\/* cannot be instantiated *\/<\/p>\n

throw new UnsupportedOperationException(\"cannot be instantiated\");<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u5224\u65adSDCard\u662f\u5426\u53ef\u7528<\/p>\n

*<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static boolean isSDCardEnable()<\/p>\n

{
\n <\/p>\n

return Environment.getExternalStorageState().equals(<\/p>\n

Environment.MEDIA_MOUNTED);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u53d6SD\u5361\u8def\u5f84<\/p>\n

*<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String getSDCardPath()<\/p>\n

{
\n <\/p>\n

return Environment.getExternalStorageDirectory().getAbsolutePath()<\/p>\n

+ File.separator;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u53d6SD\u5361\u7684\u5269\u4f59\u5bb9\u91cf \u5355\u4f4dbyte<\/p>\n

*<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static long getSDCardAllSize()<\/p>\n

{
\n <\/p>\n

if (isSDCardEnable())<\/p>\n

{
\n <\/p>\n

StatFs stat = new StatFs(getSDCardPath());<\/p>\n

\/\/ \u83b7\u53d6\u7a7a\u95f2\u7684\u6570\u636e\u5757\u7684\u6570\u91cf<\/p>\n

long availableBlocks = (long) stat.getAvailableBlocks() - ;<\/p>\n

\/\/ \u83b7\u53d6\u5355\u4e2a\u6570\u636e\u5757\u7684\u5927\u5c0f(byte)<\/p>\n

long freeBlocks = stat.getAvailableBlocks();<\/p>\n

return freeBlocks * availableBlocks;<\/p>\n

}<\/p>\n

return ;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u53d6\u6307\u5b9a\u8def\u5f84\u6240\u5728\u7a7a\u95f4\u7684\u5269\u4f59\u53ef\u7528\u5bb9\u91cf\u5b57\u8282\u6570\uff0c\u5355\u4f4dbyte<\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @return \u5bb9\u91cf\u5b57\u8282 SDCard\u53ef\u7528\u7a7a\u95f4\uff0c\u5185\u90e8\u5b58\u50a8\u53ef\u7528\u7a7a\u95f4<\/p>\n

*\/<\/p>\n

public static long getFreeBytes(String filePath)<\/p>\n

{
\n <\/p>\n

\/\/ \u5982\u679c\u662fsd\u5361\u7684\u4e0b\u7684\u8def\u5f84\uff0c\u5219\u83b7\u53d6sd\u5361\u53ef\u7528\u5bb9\u91cf<\/p>\n

if (filePath.startsWith(getSDCardPath()))<\/p>\n

{
\n <\/p>\n

filePath = getSDCardPath();<\/p>\n

} else<\/p>\n

{\/\/ \u5982\u679c\u662f\u5185\u90e8\u5b58\u50a8\u7684\u8def\u5f84\uff0c\u5219\u83b7\u53d6\u5185\u5b58\u5b58\u50a8\u7684\u53ef\u7528\u5bb9\u91cf<\/p>\n

filePath = Environment.getDataDirectory().getAbsolutePath();<\/p>\n

}<\/p>\n

StatFs stat = new StatFs(filePath);<\/p>\n

long availableBlocks = (long) stat.getAvailableBlocks() - ;<\/p>\n

return stat.getBlockSize() * availableBlocks;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u53d6\u7cfb\u7edf\u5b58\u50a8\u8def\u5f84<\/p>\n

*<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String getRootDirectoryPath()<\/p>\n

{
\n <\/p>\n

return Environment.getRootDirectory().getAbsolutePath();<\/p>\n

}<\/p>\n

}<\/p>\n

StatFs \u662fAndroid\u63d0\u4f9b\u7684\u4e00\u4e2a\u7c7b\uff1a<\/p>\n

Retrieve overall information about the space on a filesystem. This is a wrapper for Unix statvfs().<\/p>\n

\u68c0\u7d22\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf\u7684\u6574\u4f53\u4fe1\u606f\u7a7a\u95f4\u3002\u8fd9\u662f\u4e00\u4e2aUnix statvfs() \u5305\u88c5\u5668<\/p>\n

\u516d\u3001\u5c4f\u5e55\u76f8\u5173\u8f85\u52a9\u7c7b ScreenUtils.java<\/p>\n

public class ScreenUtils<\/p>\n

{
\n <\/p>\n

private ScreenUtils()<\/p>\n

{
\n <\/p>\n

\/* cannot be instantiated *\/<\/p>\n

throw new UnsupportedOperationException(\"cannot be instantiated\");<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u5f97\u5c4f\u5e55\u9ad8\u5ea6<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static int getScreenWidth(Context context)<\/p>\n

{
\n <\/p>\n

WindowManager wm = (WindowManager) context<\/p>\n

.getSystemService(Context.WINDOW_SERVICE);<\/p>\n

DisplayMetrics outMetrics = new DisplayMetrics();<\/p>\n

wm.getDefaultDisplay().getMetrics(outMetrics);<\/p>\n

return outMetrics.widthPixels;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u5f97\u5c4f\u5e55\u5bbd\u5ea6<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static int getScreenHeight(Context context)<\/p>\n

{
\n <\/p>\n

WindowManager wm = (WindowManager) context<\/p>\n

.getSystemService(Context.WINDOW_SERVICE);<\/p>\n

DisplayMetrics outMetrics = new DisplayMetrics();<\/p>\n

wm.getDefaultDisplay().getMetrics(outMetrics);<\/p>\n

return outMetrics.heightPixels;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u5f97\u72b6\u6001\u680f\u7684\u9ad8\u5ea6<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static int getStatusHeight(Context context)<\/p>\n

{
\n <\/p>\n

int statusHeight = -;<\/p>\n

try<\/p>\n

{
\n <\/p>\n

Class> clazz = Class.forName(\"com.android.internal.R$dimen\");<\/p>\n

Object object = clazz.newInstance();<\/p>\n

int height = Integer.parseInt(clazz.getField(\"status_bar_height\")<\/p>\n

.get(object).toString());<\/p>\n

statusHeight = context.getResources().getDimensionPixelSize(height);<\/p>\n

} catch (Exception e)<\/p>\n

{
\n <\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

return statusHeight;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u53d6\u5f53\u524d\u5c4f\u5e55\u622a\u56fe\uff0c\u5305\u542b\u72b6\u6001\u680f<\/p>\n

*<\/p>\n

* @param activity<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static Bitmap snapShotWithStatusBar(Activity activity)<\/p>\n

{
\n <\/p>\n

View view = activity.getWindow().getDecorView();<\/p>\n

view.setDrawingCacheEnabled(true);<\/p>\n

view.buildDrawingCache();<\/p>\n

Bitmap bmp = view.getDrawingCache();<\/p>\n

int width = getScreenWidth(activity);<\/p>\n

int height = getScreenHeight(activity);<\/p>\n

Bitmap bp = null;<\/p>\n

bp = Bitmap.createBitmap(bmp, , , width, height);<\/p>\n

view.destroyDrawingCache();<\/p>\n

return bp;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u53d6\u5f53\u524d\u5c4f\u5e55\u622a\u56fe\uff0c\u4e0d\u5305\u542b\u72b6\u6001\u680f<\/p>\n

*<\/p>\n

* @param activity<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static Bitmap snapShotWithoutStatusBar(Activity activity)<\/p>\n

{
\n <\/p>\n

View view = activity.getWindow().getDecorView();<\/p>\n

view.setDrawingCacheEnabled(true);<\/p>\n

view.buildDrawingCache();<\/p>\n

Bitmap bmp = view.getDrawingCache();<\/p>\n

Rect frame = new Rect();<\/p>\n

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);<\/p>\n

int statusBarHeight = frame.top;<\/p>\n

int width = getScreenWidth(activity);<\/p>\n

int height = getScreenHeight(activity);<\/p>\n

Bitmap bp = null;<\/p>\n

bp = Bitmap.createBitmap(bmp, , statusBarHeight, width, height<\/p>\n

- statusBarHeight);<\/p>\n

view.destroyDrawingCache();<\/p>\n

return bp;<\/p>\n

}<\/p>\n

}<\/p>\n

\u4e03\u3001App\u76f8\u5173\u8f85\u52a9\u7c7b APPUtils.java<\/p>\n

public class AppUtils<\/p>\n

{
\n <\/p>\n

private AppUtils()<\/p>\n

{
\n <\/p>\n

\/* cannot be instantiated *\/<\/p>\n

throw new UnsupportedOperationException(\"cannot be instantiated\");<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u540d\u79f0<\/p>\n

*\/<\/p>\n

public static String getAppName(Context context)<\/p>\n

{
\n <\/p>\n

try<\/p>\n

{
\n <\/p>\n

PackageManager packageManager = context.getPackageManager();<\/p>\n

PackageInfo packageInfo = packageManager.getPackageInfo(<\/p>\n

context.getPackageName(), );<\/p>\n

int labelRes = packageInfo.applicationInfo.labelRes;<\/p>\n

return context.getResources().getString(labelRes);<\/p>\n

} catch (NameNotFoundException e)<\/p>\n

{
\n <\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

return null;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* [\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u7248\u672c\u540d\u79f0\u4fe1\u606f]<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @return \u5f53\u524d\u5e94\u7528\u7684\u7248\u672c\u540d\u79f0<\/p>\n

*\/<\/p>\n

public static String getVersionName(Context context)<\/p>\n

{
\n <\/p>\n

try<\/p>\n

{
\n <\/p>\n

PackageManager packageManager = context.getPackageManager();<\/p>\n

PackageInfo packageInfo = packageManager.getPackageInfo(<\/p>\n

context.getPackageName(), );<\/p>\n

return packageInfo.versionName;<\/p>\n

} catch (NameNotFoundException e)<\/p>\n

{
\n <\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

return null;<\/p>\n

}<\/p>\n

}<\/p>\n

\u516b\u3001\u8f6f\u952e\u76d8\u76f8\u5173\u8f85\u52a9\u7c7bKeyBoardUtils.java<\/p>\n

\/**<\/p>\n

* \u6253\u5f00\u6216\u5173\u95ed\u8f6f\u952e\u76d8<\/p>\n

*\/<\/p>\n

public class KeyBoardUtils<\/p>\n

{
\n <\/p>\n

\/**<\/p>\n

* \u6253\u5361\u8f6f\u952e\u76d8<\/p>\n

*<\/p>\n

* @param mEditText \u8f93\u5165\u6846<\/p>\n

* @param mContext \u4e0a\u4e0b\u6587<\/p>\n

*\/<\/p>\n

public static void openKeybord(EditText mEditText, Context mContext)<\/p>\n

{
\n <\/p>\n

InputMethodManager imm = (InputMethodManager) mContext<\/p>\n

.getSystemService(Context.INPUT_METHOD_SERVICE);<\/p>\n

imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);<\/p>\n

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,<\/p>\n

InputMethodManager.HIDE_IMPLICIT_ONLY);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u5173\u95ed\u8f6f\u952e\u76d8<\/p>\n

*<\/p>\n

* @param mEditText \u8f93\u5165\u6846<\/p>\n

* @param mContext \u4e0a\u4e0b\u6587<\/p>\n

*\/<\/p>\n

public static void closeKeybord(EditText mEditText, Context mContext)<\/p>\n

{
\n <\/p>\n

InputMethodManager imm = (InputMethodManager) mContext<\/p>\n

.getSystemService(Context.INPUT_METHOD_SERVICE);<\/p>\n

imm.hideSoftInputFromWindow(mEditText.getWindowToken(), );<\/p>\n

}<\/p>\n

}<\/p>\n

\u4e5d\u3001\u7f51\u7edc\u76f8\u5173\u8f85\u52a9\u7c7b NetUtils.java<\/p>\n

public class NetUtils<\/p>\n

{
\n <\/p>\n

private NetUtils()<\/p>\n

{
\n <\/p>\n

\/* cannot be instantiated *\/<\/p>\n

throw new UnsupportedOperationException(\"cannot be instantiated\");<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u5224\u65ad\u7f51\u7edc\u662f\u5426\u8fde\u63a5<\/p>\n

*\/<\/p>\n

public static boolean isConnected(Context context)<\/p>\n

{
\n <\/p>\n

ConnectivityManager connectivity = (ConnectivityManager) context<\/p>\n

.getSystemService(Context.CONNECTIVITY_SERVICE);<\/p>\n

if (null != connectivity)<\/p>\n

{
\n <\/p>\n

NetworkInfo info = connectivity.getActiveNetworkInfo();<\/p>\n

if (null != info && info.isConnected())<\/p>\n

{
\n <\/p>\n

if (info.getState() == NetworkInfo.State.CONNECTED)<\/p>\n

{
\n <\/p>\n

return true;<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

return false;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u5224\u65ad\u662f\u5426\u662fwifi\u8fde\u63a5<\/p>\n

*\/<\/p>\n

public static boolean isWifi(Context context)<\/p>\n

{
\n <\/p>\n

ConnectivityManager cm = (ConnectivityManager) context<\/p>\n

.getSystemService(Context.CONNECTIVITY_SERVICE);<\/p>\n

if (cm == null)<\/p>\n

return false;<\/p>\n

return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u6253\u5f00\u7f51\u7edc\u8bbe\u7f6e\u754c\u9762<\/p>\n

*\/<\/p>\n

public static void openSetting(Activity activity)<\/p>\n

{
\n <\/p>\n

Intent intent = new Intent(\"\/\");<\/p>\n

ComponentName cm = new ComponentName(\"com.android.settings\",<\/p>\n

\"com.android.settings.WirelessSettings\");<\/p>\n

intent.setComponent(cm);<\/p>\n

intent.setAction(\"android.intent.action.VIEW\");<\/p>\n

activity.startActivityForResult(intent, );<\/p>\n

}<\/p>\n

}<\/p>\n

\u5341\u3001Http\u76f8\u5173\u8f85\u52a9\u7c7b HttpUtils.java<\/p>\n

\/**<\/p>\n

* Http\u8bf7\u6c42\u7684\u5de5\u5177\u7c7b<\/p>\n

*\/<\/p>\n

public class HttpUtils<\/p>\n

{
\n <\/p>\n

private static final int TIMEOUT_IN_MILLIONS = ;<\/p>\n

public interface CallBack<\/p>\n

{
\n <\/p>\n

void onRequestComplete(String result);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u5f02\u6b65\u7684Get\u8bf7\u6c42<\/p>\n

*<\/p>\n

* @param urlStr<\/p>\n

* @param callBack<\/p>\n

*\/<\/p>\n

public static void doGetAsyn(final String urlStr, final CallBack callBack)<\/p>\n

{
\n <\/p>\n

new Thread()<\/p>\n

{
\n <\/p>\n

public void run()<\/p>\n

{
\n <\/p>\n

try<\/p>\n

{
\n <\/p>\n

String result = doGet(urlStr);<\/p>\n

if (callBack != null)<\/p>\n

{
\n <\/p>\n

callBack.onRequestComplete(result);<\/p>\n

}<\/p>\n

} catch (Exception e)<\/p>\n

{
\n <\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

};<\/p>\n

}.start();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u5f02\u6b65\u7684Post\u8bf7\u6c42<\/p>\n

* @param urlStr<\/p>\n

* @param params<\/p>\n

* @param callBack<\/p>\n

* @throws Exception<\/p>\n

*\/<\/p>\n

public static void doPostAsyn(final String urlStr, final String params,<\/p>\n

final CallBack callBack) throws Exception<\/p>\n

{
\n <\/p>\n

new Thread()<\/p>\n

{
\n <\/p>\n

public void run()<\/p>\n

{
\n <\/p>\n

try<\/p>\n

{
\n <\/p>\n

String result = doPost(urlStr, params);<\/p>\n

if (callBack != null)<\/p>\n

{
\n <\/p>\n

callBack.onRequestComplete(result);<\/p>\n

}<\/p>\n

} catch (Exception e)<\/p>\n

{
\n <\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

};<\/p>\n

}.start();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* Get\u8bf7\u6c42\uff0c\u83b7\u5f97\u8fd4\u56de\u6570\u636e<\/p>\n

*<\/p>\n

* @param urlStr<\/p>\n

* @return<\/p>\n

* @throws Exception<\/p>\n

*\/<\/p>\n

public static String doGet(String urlStr)<\/p>\n

{
\n <\/p>\n

URL url = null;<\/p>\n

HttpURLConnection conn = null;<\/p>\n

InputStream is = null;<\/p>\n

ByteArrayOutputStream baos = null;<\/p>\n

try<\/p>\n

{
\n <\/p>\n

url = new URL(urlStr);<\/p>\n

conn = (HttpURLConnection) url.openConnection();<\/p>\n

conn.setReadTimeout(TIMEOUT_IN_MILLIONS);<\/p>\n

conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);<\/p>\n

conn.setRequestMethod(\"GET\");<\/p>\n

conn.setRequestProperty(\"accept\", \"*\/*\");<\/p>\n

conn.setRequestProperty(\"connection\", \"Keep-Alive\");<\/p>\n

if (conn.getResponseCode() == )<\/p>\n

{
\n <\/p>\n

is = conn.getInputStream();<\/p>\n

baos = new ByteArrayOutputStream();<\/p>\n

int len = -;<\/p>\n

byte[] buf = new byte[];<\/p>\n

while ((len = is.read(buf)) != -)<\/p>\n

{
\n <\/p>\n

baos.write(buf, , len);<\/p>\n

}<\/p>\n

baos.flush();<\/p>\n

return baos.toString();<\/p>\n

} else<\/p>\n

{
\n <\/p>\n

throw new RuntimeException(\" responseCode is not ... \");<\/p>\n

}<\/p>\n

} catch (Exception e)<\/p>\n

{
\n <\/p>\n

e.printStackTrace();<\/p>\n

} finally<\/p>\n

{
\n <\/p>\n

try<\/p>\n

{
\n <\/p>\n

if (is != null)<\/p>\n

is.close();<\/p>\n

} catch (IOException e)<\/p>\n

{
\n <\/p>\n

}<\/p>\n

try<\/p>\n

{
\n <\/p>\n

if (baos != null)<\/p>\n

baos.close();<\/p>\n

} catch (IOException e)<\/p>\n

{
\n <\/p>\n

}<\/p>\n

conn.disconnect();<\/p>\n

}<\/p>\n

return null ;<\/p>\n

}<\/p>\n

\/**<\/p>\n

* \u5411\u6307\u5b9a URL \u53d1\u9001POST\u65b9\u6cd5\u7684\u8bf7\u6c42<\/p>\n

*<\/p>\n

* @param url<\/p>\n

* \u53d1\u9001\u8bf7\u6c42\u7684 URL<\/p>\n

* @param param<\/p>\n

* \u8bf7\u6c42\u53c2\u6570\uff0c\u8bf7\u6c42\u53c2\u6570\u5e94\u8be5\u662f name=value&name=value \u7684\u5f62\u5f0f\u3002<\/p>\n

* @return \u6240\u4ee3\u8868\u8fdc\u7a0b\u8d44\u6e90\u7684\u54cd\u5e94\u7ed3\u679c<\/p>\n

* @throws Exception<\/p>\n

*\/<\/p>\n

public static String doPost(String url, String param)<\/p>\n

{
\n <\/p>\n

PrintWriter out = null;<\/p>\n

BufferedReader in = null;<\/p>\n

String result = \"\";<\/p>\n

try<\/p>\n

{
\n <\/p>\n

URL realUrl = new URL(url);<\/p>\n

\/\/ \u6253\u5f00\u548cURL\u4e4b\u95f4\u7684\u8fde\u63a5<\/p>\n

HttpURLConnection conn = (HttpURLConnection) realUrl<\/p>\n

.openConnection();<\/p>\n

\/\/ \u8bbe\u7f6e\u901a\u7528\u7684\u8bf7\u6c42\u5c5e\u6027<\/p>\n

conn.setRequestProperty(\"accept\", \"*\/*\");<\/p>\n

conn.setRequestProperty(\"connection\", \"Keep-Alive\");<\/p>\n

conn.setRequestMethod(\"POST\");<\/p>\n

conn.setRequestProperty(\"Content-Type\",<\/p>\n

\"application\/x-www-form-urlencoded\");<\/p>\n

conn.setRequestProperty(\"charset\", \"utf-\");<\/p>\n

conn.setUseCaches(false);<\/p>\n

\/\/ \u53d1\u9001POST\u8bf7\u6c42\u5fc5\u987b\u8bbe\u7f6e\u5982\u4e0b\u4e24\u884c<\/p>\n

conn.setDoOutput(true);<\/p>\n

conn.setDoInput(true);<\/p>\n

conn.setReadTimeout(TIMEOUT_IN_MILLIONS);<\/p>\n

conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);<\/p>\n

if (param != null && !param.trim().equals(\"\"))<\/p>\n

{
\n <\/p>\n

\/\/ \u83b7\u53d6URLConnection\u5bf9\u8c61\u5bf9\u5e94\u7684\u8f93\u51fa\u6d41<\/p>\n

out = new PrintWriter(conn.getOutputStream());<\/p>\n

\/\/ \u53d1\u9001\u8bf7\u6c42\u53c2\u6570<\/p>\n

out.print(param);<\/p>\n

\/\/ flush\u8f93\u51fa\u6d41\u7684\u7f13\u51b2<\/p>\n

out.flush();<\/p>\n

}<\/p>\n

\/\/ \u5b9a\u4e49BufferedReader\u8f93\u5165\u6d41\u6765\u8bfb\u53d6URL\u7684\u54cd\u5e94<\/p>\n

in = new BufferedReader(<\/p>\n

new InputStreamReader(conn.getInputStream()));<\/p>\n

String line;<\/p>\n

while ((line = in.readLine()) != null)<\/p>\n

{
\n <\/p>\n

result += line;<\/p>\n

}<\/p>\n

} catch (Exception e)<\/p>\n

{
\n <\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

\/\/ \u4f7f\u7528finally\u5757\u6765\u5173\u95ed\u8f93\u51fa\u6d41\u3001\u8f93\u5165\u6d41<\/p>\n

finally<\/p>\n

{
\n <\/p>\n

try<\/p>\n

{
\n <\/p>\n

if (out != null)<\/p>\n

{
\n <\/p>\n

out.close();<\/p>\n

}<\/p>\n

if (in != null)<\/p>\n

{
\n <\/p>\n

in.close();<\/p>\n

}<\/p>\n

} catch (IOException ex)<\/p>\n

{
\n <\/p>\n

ex.printStackTrace();<\/p>\n

}<\/p>\n

}<\/p>\n

return result;<\/p>\n

}<\/p>\n

}<\/p>\n

\u5341\u4e00\u3001\u65f6\u95f4\u5de5\u5177\u7c7b TimeUtils.java<\/p>\n

public class TimeUtils {
\n <\/p>\n

public static final SimpleDateFormat DEFAULT_DATE_FORMAT =<\/p>\n

new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");<\/p>\n

public static final SimpleDateFormat DATE_FORMAT_DATE =<\/p>\n

new SimpleDateFormat(\"yyyy-MM-dd\");<\/p>\n

private TimeUtils() {
\n <\/p>\n

throw new AssertionError();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* long time to string<\/p>\n

*<\/p>\n

* @param timeInMillis<\/p>\n

* @param dateFormat<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
\n <\/p>\n

return dateFormat.format(new Date(timeInMillis));<\/p>\n

}<\/p>\n

\/**<\/p>\n

* long time to string, format is {@link #DEFAULT_DATE_FORMAT}<\/p>\n

*<\/p>\n

* @param timeInMillis<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String getTime(long timeInMillis) {
\n <\/p>\n

return getTime(timeInMillis, DEFAULT_DATE_FORMAT);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get current time in milliseconds<\/p>\n

*<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static long getCurrentTimeInLong() {
\n <\/p>\n

return System.currentTimeMillis();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}<\/p>\n

*<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String getCurrentTimeInString() {
\n <\/p>\n

return getTime(getCurrentTimeInLong());<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get current time in milliseconds<\/p>\n

*<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
\n <\/p>\n

return getTime(getCurrentTimeInLong(), dateFormat);<\/p>\n

}<\/p>\n

}<\/p>\n

\u5341\u4e8c\u3001\u6587\u4ef6\u5de5\u5177\u7c7b FileUtils.java<\/p>\n

public class FileUtils {
\n <\/p>\n

public final static String FILE_EXTENSION_SEPARATOR = \".\";<\/p>\n

private FileUtils() {
\n <\/p>\n

throw new AssertionError();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* read file<\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @param charsetName The name of a supported {@link java.nio.charset.Charset charset}<\/code><\/p>\n

* @return if file not exist, return null, else return content of file<\/p>\n

* @throws RuntimeException if an error occurs while operator BufferedReader<\/p>\n

*\/<\/p>\n

public static StringBuilder readFile(String filePath, String charsetName) {
\n <\/p>\n

File file = new File(filePath);<\/p>\n

StringBuilder fileContent = new StringBuilder(\"\");<\/p>\n

if (file == null || !file.isFile()) {
\n <\/p>\n

return null;<\/p>\n

}<\/p>\n

BufferedReader reader = null;<\/p>\n

try {
\n <\/p>\n

InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);<\/p>\n

reader = new BufferedReader(is);<\/p>\n

String line = null;<\/p>\n

while ((line = reader.readLine()) != null) {
\n <\/p>\n

if (!fileContent.toString().equals(\"\")) {
\n <\/p>\n

fileContent.append(\"\\r\\n\");<\/p>\n

}<\/p>\n

fileContent.append(line);<\/p>\n

}<\/p>\n

return fileContent;<\/p>\n

} catch (IOException e) {
\n <\/p>\n

throw new RuntimeException(\"IOException occurred. \", e);<\/p>\n

} finally {
\n <\/p>\n

IOUtils.close(reader);<\/p>\n

}<\/p>\n

}<\/p>\n

\/**<\/p>\n

* write file<\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @param content<\/p>\n

* @param append is append, if true, write to the end of file, else clear content of file and write into it<\/p>\n

* @return return false if content is empty, true otherwise<\/p>\n

* @throws RuntimeException if an error occurs while operator FileWriter<\/p>\n

*\/<\/p>\n

public static boolean writeFile(String filePath, String content, boolean append) {
\n <\/p>\n

if (StringUtils.isEmpty(content)) {
\n <\/p>\n

return false;<\/p>\n

}<\/p>\n

FileWriter fileWriter = null;<\/p>\n

try {
\n <\/p>\n

makeDirs(filePath);<\/p>\n

fileWriter = new FileWriter(filePath, append);<\/p>\n

fileWriter.write(content);<\/p>\n

return true;<\/p>\n

} catch (IOException e) {
\n <\/p>\n

throw new RuntimeException(\"IOException occurred. \", e);<\/p>\n

} finally {
\n <\/p>\n

IOUtils.close(fileWriter);<\/p>\n

}<\/p>\n

}<\/p>\n

\/**<\/p>\n

* write file<\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @param contentList<\/p>\n

* @param append is append, if true, write to the end of file, else clear content of file and write into it<\/p>\n

* @return return false if contentList is empty, true otherwise<\/p>\n

* @throws RuntimeException if an error occurs while operator FileWriter<\/p>\n

*\/<\/p>\n

public static boolean writeFile(String filePath, List contentList, boolean append) {
\n <\/p>\n

if (ListUtils.isEmpty(contentList)) {
\n <\/p>\n

return false;<\/p>\n

}<\/p>\n

FileWriter fileWriter = null;<\/p>\n

try {
\n <\/p>\n

makeDirs(filePath);<\/p>\n

fileWriter = new FileWriter(filePath, append);<\/p>\n

int i = ;<\/p>\n

for (String line : contentList) {
\n <\/p>\n

if (i++ > ) {
\n <\/p>\n

fileWriter.write(\"\\r\\n\");<\/p>\n

}<\/p>\n

fileWriter.write(line);<\/p>\n

}<\/p>\n

return true;<\/p>\n

} catch (IOException e) {
\n <\/p>\n

throw new RuntimeException(\"IOException occurred. \", e);<\/p>\n

} finally {
\n <\/p>\n

IOUtils.close(fileWriter);<\/p>\n

}<\/p>\n

}<\/p>\n

\/**<\/p>\n

* write file, the string will be written to the begin of the file<\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @param content<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static boolean writeFile(String filePath, String content) {
\n <\/p>\n

return writeFile(filePath, content, false);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* write file, the string list will be written to the begin of the file<\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @param contentList<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static boolean writeFile(String filePath, List contentList) {
\n <\/p>\n

return writeFile(filePath, contentList, false);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* write file, the bytes will be written to the begin of the file<\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @param stream<\/p>\n

* @return<\/p>\n

* @see {@link #writeFile(String, InputStream, boolean)}<\/p>\n

*\/<\/p>\n

public static boolean writeFile(String filePath, InputStream stream) {
\n <\/p>\n

return writeFile(filePath, stream, false);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* write file<\/p>\n

*<\/p>\n

* @param file the file to be opened for writing.<\/p>\n

* @param stream the input stream<\/p>\n

* @param append if true<\/code>, then bytes will be written to the end of the file rather than the beginning<\/p>\n

* @return return true<\/p>\n

* @throws RuntimeException if an error occurs while operator FileOutputStream<\/p>\n

*\/<\/p>\n

public static boolean writeFile(String filePath, InputStream stream, boolean append) {
\n <\/p>\n

return writeFile(filePath != null ? new File(filePath) : null, stream, append);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* write file, the bytes will be written to the begin of the file<\/p>\n

*<\/p>\n

* @param file<\/p>\n

* @param stream<\/p>\n

* @return<\/p>\n

* @see {@link #writeFile(File, InputStream, boolean)}<\/p>\n

*\/<\/p>\n

public static boolean writeFile(File file, InputStream stream) {
\n <\/p>\n

return writeFile(file, stream, false);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* write file<\/p>\n

*<\/p>\n

* @param file the file to be opened for writing.<\/p>\n

* @param stream the input stream<\/p>\n

* @param append if true<\/code>, then bytes will be written to the end of the file rather than the beginning<\/p>\n

* @return return true<\/p>\n

* @throws RuntimeException if an error occurs while operator FileOutputStream<\/p>\n

*\/<\/p>\n

public static boolean writeFile(File file, InputStream stream, boolean append) {
\n <\/p>\n

OutputStream o = null;<\/p>\n

try {
\n <\/p>\n

makeDirs(file.getAbsolutePath());<\/p>\n

o = new FileOutputStream(file, append);<\/p>\n

byte data[] = new byte[];<\/p>\n

int length = -;<\/p>\n

while ((length = stream.read(data)) != -) {
\n <\/p>\n

o.write(data, , length);<\/p>\n

}<\/p>\n

o.flush();<\/p>\n

return true;<\/p>\n

} catch (FileNotFoundException e) {
\n <\/p>\n

throw new RuntimeException(\"FileNotFoundException occurred. \", e);<\/p>\n

} catch (IOException e) {
\n <\/p>\n

throw new RuntimeException(\"IOException occurred. \", e);<\/p>\n

} finally {
\n <\/p>\n

IOUtils.close(o);<\/p>\n

IOUtils.close(stream);<\/p>\n

}<\/p>\n

}<\/p>\n

\/**<\/p>\n

* move file<\/p>\n

*<\/p>\n

* @param sourceFilePath<\/p>\n

* @param destFilePath<\/p>\n

*\/<\/p>\n

public static void moveFile(String sourceFilePath, String destFilePath) {
\n <\/p>\n

if (TextUtils.isEmpty(sourceFilePath) || TextUtils.isEmpty(destFilePath)) {
\n <\/p>\n

throw new RuntimeException(\"Both sourceFilePath and destFilePath cannot be null.\");<\/p>\n

}<\/p>\n

moveFile(new File(sourceFilePath), new File(destFilePath));<\/p>\n

}<\/p>\n

\/**<\/p>\n

* move file<\/p>\n

*<\/p>\n

* @param srcFile<\/p>\n

* @param destFile<\/p>\n

*\/<\/p>\n

public static void moveFile(File srcFile, File destFile) {
\n <\/p>\n

boolean rename = srcFile.renameTo(destFile);<\/p>\n

if (!rename) {
\n <\/p>\n

copyFile(srcFile.getAbsolutePath(), destFile.getAbsolutePath());<\/p>\n

deleteFile(srcFile.getAbsolutePath());<\/p>\n

}<\/p>\n

}<\/p>\n

\/**<\/p>\n

* copy file<\/p>\n

*<\/p>\n

* @param sourceFilePath<\/p>\n

* @param destFilePath<\/p>\n

* @return<\/p>\n

* @throws RuntimeException if an error occurs while operator FileOutputStream<\/p>\n

*\/<\/p>\n

public static boolean copyFile(String sourceFilePath, String destFilePath) {
\n <\/p>\n

InputStream inputStream = null;<\/p>\n

try {
\n <\/p>\n

inputStream = new FileInputStream(sourceFilePath);<\/p>\n

} catch (FileNotFoundException e) {
\n <\/p>\n

throw new RuntimeException(\"FileNotFoundException occurred. \", e);<\/p>\n

}<\/p>\n

return writeFile(destFilePath, inputStream);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* read file to string list, a element of list is a line<\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @param charsetName The name of a supported {@link java.nio.charset.Charset charset}<\/code><\/p>\n

* @return if file not exist, return null, else return content of file<\/p>\n

* @throws RuntimeException if an error occurs while operator BufferedReader<\/p>\n

*\/<\/p>\n

public static List readFileToList(String filePath, String charsetName) {
\n <\/p>\n

File file = new File(filePath);<\/p>\n

List fileContent = new ArrayList();<\/p>\n

if (file == null || !file.isFile()) {
\n <\/p>\n

return null;<\/p>\n

}<\/p>\n

BufferedReader reader = null;<\/p>\n

try {
\n <\/p>\n

InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);<\/p>\n

reader = new BufferedReader(is);<\/p>\n

String line = null;<\/p>\n

while ((line = reader.readLine()) != null) {
\n <\/p>\n

fileContent.add(line);<\/p>\n

}<\/p>\n

return fileContent;<\/p>\n

} catch (IOException e) {
\n <\/p>\n

throw new RuntimeException(\"IOException occurred. \", e);<\/p>\n

} finally {
\n <\/p>\n

IOUtils.close(reader);<\/p>\n

}<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get file name from path, not include suffix<\/p>\n

*<\/p>\n

* <\/p>\n

<\/pre>\n

* getFileNameWithoutExtension(null) = null<\/p>\n

* getFileNameWithoutExtension(\"\") = \"\"<\/p>\n

* getFileNameWithoutExtension(\" \") = \" \"<\/p>\n

* getFileNameWithoutExtension(\"abc\") = \"abc\"<\/p>\n

* getFileNameWithoutExtension(\"a.mp\") = \"a\"<\/p>\n

* getFileNameWithoutExtension(\"a.b.rmvb\") = \"a.b\"<\/p>\n

* getFileNameWithoutExtension(\"c:\\\\\") = \"\"<\/p>\n

* getFileNameWithoutExtension(\"c:\\\\a\") = \"a\"<\/p>\n

* getFileNameWithoutExtension(\"c:\\\\a.b\") = \"a\"<\/p>\n

* getFileNameWithoutExtension(\"c:a.txt\\\\a\") = \"a\"<\/p>\n

* getFileNameWithoutExtension(\"\/home\/admin\") = \"admin\"<\/p>\n

* getFileNameWithoutExtension(\"\/home\/admin\/a.txt\/b.mp\") = \"b\"<\/p>\n

* <\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @return file name from path, not include suffix<\/p>\n

* @see<\/p>\n

*\/<\/p>\n

public static String getFileNameWithoutExtension(String filePath) {
\n <\/p>\n

if (StringUtils.isEmpty(filePath)) {
\n <\/p>\n

return filePath;<\/p>\n

}<\/p>\n

int extenPosi = filePath.lastIndexOf(FILE_EXTENSION_SEPARATOR);<\/p>\n

int filePosi = filePath.lastIndexOf(File.separator);<\/p>\n

if (filePosi == -) {
\n <\/p>\n

return (extenPosi == - ? filePath : filePath.substring(, extenPosi));<\/p>\n

}<\/p>\n

if (extenPosi == -) {
\n <\/p>\n

return filePath.substring(filePosi + );<\/p>\n

}<\/p>\n

return (filePosi < extenPosi ? filePath.substring(filePosi + , extenPosi) : filePath.substring(filePosi + ));<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get file name from path, include suffix<\/p>\n

*<\/p>\n

* <\/p>\n

<\/pre>\n

* getFileName(null) = null<\/p>\n

* getFileName(\"\") = \"\"<\/p>\n

* getFileName(\" \") = \" \"<\/p>\n

* getFileName(\"a.mp\") = \"a.mp\"<\/p>\n

* getFileName(\"a.b.rmvb\") = \"a.b.rmvb\"<\/p>\n

* getFileName(\"abc\") = \"abc\"<\/p>\n

* getFileName(\"c:\\\\\") = \"\"<\/p>\n

* getFileName(\"c:\\\\a\") = \"a\"<\/p>\n

* getFileName(\"c:\\\\a.b\") = \"a.b\"<\/p>\n

* getFileName(\"c:a.txt\\\\a\") = \"a\"<\/p>\n

* getFileName(\"\/home\/admin\") = \"admin\"<\/p>\n

* getFileName(\"\/home\/admin\/a.txt\/b.mp\") = \"b.mp\"<\/p>\n

* <\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @return file name from path, include suffix<\/p>\n

*\/<\/p>\n

public static String getFileName(String filePath) {
\n <\/p>\n

if (StringUtils.isEmpty(filePath)) {
\n <\/p>\n

return filePath;<\/p>\n

}<\/p>\n

int filePosi = filePath.lastIndexOf(File.separator);<\/p>\n

return (filePosi == -) ? filePath : filePath.substring(filePosi + );<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get folder name from path<\/p>\n

*<\/p>\n

* <\/p>\n

<\/pre>\n

* getFolderName(null) = null<\/p>\n

* getFolderName(\"\") = \"\"<\/p>\n

* getFolderName(\" \") = \"\"<\/p>\n

* getFolderName(\"a.mp\") = \"\"<\/p>\n

* getFolderName(\"a.b.rmvb\") = \"\"<\/p>\n

* getFolderName(\"abc\") = \"\"<\/p>\n

* getFolderName(\"c:\\\\\") = \"c:\"<\/p>\n

* getFolderName(\"c:\\\\a\") = \"c:\"<\/p>\n

* getFolderName(\"c:\\\\a.b\") = \"c:\"<\/p>\n

* getFolderName(\"c:a.txt\\\\a\") = \"c:a.txt\"<\/p>\n

* getFolderName(\"c:a\\\\b\\\\c\\\\d.txt\") = \"c:a\\\\b\\\\c\"<\/p>\n

* getFolderName(\"\/home\/admin\") = \"\/home\"<\/p>\n

* getFolderName(\"\/home\/admin\/a.txt\/b.mp\") = \"\/home\/admin\/a.txt\"<\/p>\n

* <\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String getFolderName(String filePath) {
\n <\/p>\n

if (StringUtils.isEmpty(filePath)) {
\n <\/p>\n

return filePath;<\/p>\n

}<\/p>\n

int filePosi = filePath.lastIndexOf(File.separator);<\/p>\n

return (filePosi == -) ? \"\" : filePath.substring(, filePosi);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get suffix of file from path<\/p>\n

*<\/p>\n

* <\/p>\n

<\/pre>\n

* getFileExtension(null) = \"\"<\/p>\n

* getFileExtension(\"\") = \"\"<\/p>\n

* getFileExtension(\" \") = \" \"<\/p>\n

* getFileExtension(\"a.mp\") = \"mp\"<\/p>\n

* getFileExtension(\"a.b.rmvb\") = \"rmvb\"<\/p>\n

* getFileExtension(\"abc\") = \"\"<\/p>\n

* getFileExtension(\"c:\\\\\") = \"\"<\/p>\n

* getFileExtension(\"c:\\\\a\") = \"\"<\/p>\n

* getFileExtension(\"c:\\\\a.b\") = \"b\"<\/p>\n

* getFileExtension(\"c:a.txt\\\\a\") = \"\"<\/p>\n

* getFileExtension(\"\/home\/admin\") = \"\"<\/p>\n

* getFileExtension(\"\/home\/admin\/a.txt\/b\") = \"\"<\/p>\n

* getFileExtension(\"\/home\/admin\/a.txt\/b.mp\") = \"mp\"<\/p>\n

* <\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String getFileExtension(String filePath) {
\n <\/p>\n

if (StringUtils.isBlank(filePath)) {
\n <\/p>\n

return filePath;<\/p>\n

}<\/p>\n

int extenPosi = filePath.lastIndexOf(FILE_EXTENSION_SEPARATOR);<\/p>\n

int filePosi = filePath.lastIndexOf(File.separator);<\/p>\n

if (extenPosi == -) {
\n <\/p>\n

return \"\";<\/p>\n

}<\/p>\n

return (filePosi >= extenPosi) ? \"\" : filePath.substring(extenPosi + );<\/p>\n

}<\/p>\n

\/**<\/p>\n

* Creates the directory named by the trailing filename of this file, including the complete directory path required<\/p>\n

* to create this directory. <\/p>\n

* <\/p>\n

* <\/p>\n

* Attentions:<\/strong><\/p>\n

* <\/p>\n

makeDirs(\"C:\\\\Users\\\\Trinea\") can only create users folder <\/p>\n

* <\/p>\n

makeFolder(\"C:\\\\Users\\\\Trinea\\\\\") can create Trinea folder <\/p>\n

* <\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @return true if the necessary directories have been created or the target directory already exists, false one of<\/p>\n

* the directories can not be created.<\/p>\n

* <\/p>\n

* <\/p>\n

if {@link FileUtils#getFolderName(String)} return null, return false <\/p>\n

* <\/p>\n

if target directory already exists, return true <\/p>\n

* <\/p>\n

return {@link java.io.File#makeFolder} <\/p>\n

* <\/p>\n

*\/<\/p>\n

public static boolean makeDirs(String filePath) {
\n <\/p>\n

String folderName = getFolderName(filePath);<\/p>\n

if (StringUtils.isEmpty(folderName)) {
\n <\/p>\n

return false;<\/p>\n

}<\/p>\n

File folder = new File(folderName);<\/p>\n

return (folder.exists() && folder.isDirectory()) ? true : folder.mkdirs();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* @param filePath<\/p>\n

* @return<\/p>\n

* @see #makeDirs(String)<\/p>\n

*\/<\/p>\n

public static boolean makeFolders(String filePath) {
\n <\/p>\n

return makeDirs(filePath);<\/p>\n

}<\/p>\n

\/**<\/p>\n

* Indicates if this file represents a file on the underlying file system.<\/p>\n

*<\/p>\n

* @param filePath<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static boolean isFileExist(String filePath) {
\n <\/p>\n

if (StringUtils.isBlank(filePath)) {
\n <\/p>\n

return false;<\/p>\n

}<\/p>\n

File file = new File(filePath);<\/p>\n

return (file.exists() && file.isFile());<\/p>\n

}<\/p>\n

\/**<\/p>\n

* Indicates if this file represents a directory on the underlying file system.<\/p>\n

*<\/p>\n

* @param directoryPath<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static boolean isFolderExist(String directoryPath) {
\n <\/p>\n

if (StringUtils.isBlank(directoryPath)) {
\n <\/p>\n

return false;<\/p>\n

}<\/p>\n

File dire = new File(directoryPath);<\/p>\n

return (dire.exists() && dire.isDirectory());<\/p>\n

}<\/p>\n

\/**<\/p>\n

* delete file or directory<\/p>\n

* <\/p>\n

* <\/p>\n

if path is null or empty, return true <\/p>\n

* <\/p>\n

if path not exist, return true <\/p>\n

* <\/p>\n

if path exist, delete recursion. return true <\/p>\n

* <\/p>\n

*<\/p>\n

* @param path<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static boolean deleteFile(String path) {
\n <\/p>\n

if (StringUtils.isBlank(path)) {
\n <\/p>\n

return true;<\/p>\n

}<\/p>\n

File file = new File(path);<\/p>\n

if (!file.exists()) {
\n <\/p>\n

return true;<\/p>\n

}<\/p>\n

if (file.isFile()) {
\n <\/p>\n

return file.delete();<\/p>\n

}<\/p>\n

if (!file.isDirectory()) {
\n <\/p>\n

return false;<\/p>\n

}<\/p>\n

for (File f : file.listFiles()) {
\n <\/p>\n

if (f.isFile()) {
\n <\/p>\n

f.delete();<\/p>\n

} else if (f.isDirectory()) {
\n <\/p>\n

deleteFile(f.getAbsolutePath());<\/p>\n

}<\/p>\n

}<\/p>\n

return file.delete();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get file size<\/p>\n

* <\/p>\n

* <\/p>\n

if path is null or empty, return - <\/p>\n

* <\/p>\n

if path exist and it is a file, return file size, else return - <\/p>\n

* <\/p>\n

*<\/p>\n

* @param path<\/p>\n

* @return returns the length of this file in bytes. returns - if the file does not exist.<\/p>\n

*\/<\/p>\n

public static long getFileSize(String path) {
\n <\/p>\n

if (StringUtils.isBlank(path)) {
\n <\/p>\n

return -;<\/p>\n

}<\/p>\n

File file = new File(path);<\/p>\n

return (file.exists() && file.isFile() ? file.length() : -);<\/p>\n

}<\/p>\n

}<\/p>\n

\u5341\u4e09\u3001assets\u548craw\u8d44\u6e90\u5de5\u5177\u7c7b ResourceUtils.java<\/p>\n

public class ResourceUtils {
\n <\/p>\n

private ResourceUtils() {
\n <\/p>\n

throw new AssertionError();<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get an asset using ACCESS_STREAMING mode. This provides access to files that have been bundled with an<\/p>\n

* application as assets -- that is, files placed in to the \"assets\" directory.<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param fileName The name of the asset to open. This name can be hierarchical.<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String geFileFromAssets(Context context, String fileName) {
\n <\/p>\n

if (context == null || StringUtils.isEmpty(fileName)) {
\n <\/p>\n

return null;<\/p>\n

}<\/p>\n

StringBuilder s = new StringBuilder(\"\");<\/p>\n

try {
\n <\/p>\n

InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));<\/p>\n

BufferedReader br = new BufferedReader(in);<\/p>\n

String line;<\/p>\n

while ((line = br.readLine()) != null) {
\n <\/p>\n

s.append(line);<\/p>\n

}<\/p>\n

return s.toString();<\/p>\n

} catch (IOException e) {
\n <\/p>\n

e.printStackTrace();<\/p>\n

return null;<\/p>\n

}<\/p>\n

}<\/p>\n

\/**<\/p>\n

* get content from a raw resource. This can only be used with resources whose value is the name of an asset files<\/p>\n

* -- that is, it can be used to open drawable, sound, and raw resources; it will fail on string and color<\/p>\n

* resources.<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param resId The resource identifier to open, as generated by the appt tool.<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static String geFileFromRaw(Context context, int resId) {
\n <\/p>\n

if (context == null) {
\n <\/p>\n

return null;<\/p>\n

}<\/p>\n

StringBuilder s = new StringBuilder();<\/p>\n

try {
\n <\/p>\n

InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));<\/p>\n

BufferedReader br = new BufferedReader(in);<\/p>\n

String line;<\/p>\n

while ((line = br.readLine()) != null) {
\n <\/p>\n

s.append(line);<\/p>\n

}<\/p>\n

return s.toString();<\/p>\n

} catch (IOException e) {
\n <\/p>\n

e.printStackTrace();<\/p>\n

return null;<\/p>\n

}<\/p>\n

}<\/p>\n

\/**<\/p>\n

* same to {@link ResourceUtils#geFileFromAssets(Context, String)}, but return type is List<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param fileName<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static List geFileToListFromAssets(Context context, String fileName) {
\n <\/p>\n

if (context == null || StringUtils.isEmpty(fileName)) {
\n <\/p>\n

return null;<\/p>\n

}<\/p>\n

List fileContent = new ArrayList();<\/p>\n

try {
\n <\/p>\n

InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));<\/p>\n

BufferedReader br = new BufferedReader(in);<\/p>\n

String line;<\/p>\n

while ((line = br.readLine()) != null) {
\n <\/p>\n

fileContent.add(line);<\/p>\n

}<\/p>\n

br.close();<\/p>\n

return fileContent;<\/p>\n

} catch (IOException e) {
\n <\/p>\n

e.printStackTrace();<\/p>\n

return null;<\/p>\n

}<\/p>\n

}<\/p>\n

\/**<\/p>\n

* same to {@link ResourceUtils#geFileFromRaw(Context, int)}, but return type is List<\/p>\n

*<\/p>\n

* @param context<\/p>\n

* @param resId<\/p>\n

* @return<\/p>\n

*\/<\/p>\n

public static List geFileToListFromRaw(Context context, int resId) {
\n <\/p>\n

if (context == null) {
\n <\/p>\n

return null;<\/p>\n

}<\/p>\n

List fileContent = new ArrayList();<\/p>\n

BufferedReader reader = null;<\/p>\n

try {
\n <\/p>\n

InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));<\/p>\n

reader = new BufferedReader(in);<\/p>\n

String line = null;<\/p>\n

while ((line = reader.readLine()) != null) {
\n <\/p>\n

fileContent.add(line);<\/p>\n

}<\/p>\n

reader.close();<\/p>\n

return fileContent;<\/p>\n

} catch (IOException e) {
\n <\/p>\n

e.printStackTrace();<\/p>\n

return null;<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

\u5341\u56db\u3001\u5355\u4f8b\u5de5\u5177\u7c7b SingletonUtils.java<\/p>\n

public abstract class SingletonUtils {
\n <\/p>\n

private T instance;<\/p>\n

protected abstract T newInstance();<\/p>\n

public final T getInstance() {
\n <\/p>\n

if (instance == null) {
\n <\/p>\n

synchronized (SingletonUtils.class) {
\n <\/p>\n

if (instance == null) {
\n <\/p>\n

instance = newInstance();<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

return instance;<\/p>\n

}<\/p>\n

}<\/p>\n

\u5341\u4e94\u3001\u6570\u636e\u5e93\u5de5\u5177\u7c7b SqliteUtils.java<\/p>\n

public class SqliteUtils {
\n <\/p>\n

private static volatile SqliteUtils instance;<\/p>\n

private DbHelper dbHelper;<\/p>\n

private SQLiteDatabase db;<\/p>\n

private SqliteUtils(Context context) {
\n <\/p>\n

dbHelper = new DbHelper(context);<\/p>\n

db = dbHelper.getWritableDatabase();<\/p>\n

}<\/p>\n

public static SqliteUtils getInstance(Context context) {
\n <\/p>\n

if (instance == null) {
\n <\/p>\n

synchronized (SqliteUtils.class) {
\n <\/p>\n

if (instance == null) {
\n <\/p>\n

instance = new SqliteUtils(context);<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

return instance;<\/p>\n

}<\/p>\n

public SQLiteDatabase getDb() {
\n <\/p>\n

return db;<\/p>\n

}<\/p>\n

}<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"android app\u5feb\u901f\u5f00\u53d1_android app\u5f00\u53d1\u5de5\u5177\u4e00\u3001\u65e5\u5fd7\u5de5\u5177\u7c7bLog.javapublicclassL{privateL(){\/*\u4e0d\u53ef\u88ab\u5b9e\u4f8b\u5316*\/thrownewUnsuppor...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8170"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8170"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8170\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}