Android快速開發系列 常用工具類

來源:互聯網
上載者:User

標籤:android   快速開發   工具類   

1、日誌工具類L.java

package com.way.common.util;import android.util.Log;/** * Log統一管理類 *  * @author way *  */public class L{public static boolean isDebug = true;// 是否需要列印bug,可以在application的onCreate函數裡面初始化private static final String TAG = "way";// 下面四個是預設tag的函數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);}// 下面是傳入自訂tag的函數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);}}

網上看到的類,注釋上應該原創作者的名字,很簡單的一個類;網上也有很多提供把日誌記錄到SDCard上的,不過我是從來沒記錄過,所以引入個最簡單的,大家可以進行評價是否需要擴充~~

2、Toast統一管理類

package com.way.common.util;import android.content.Context;import android.widget.Toast;/** * Toast統一管理類 *  */public class T{public static boolean isShow = true;/** * 短時間顯示Toast *  * @param context * @param message */public static void showShort(Context context, CharSequence message){if (isShow)Toast.makeText(context, message, Toast.LENGTH_SHORT).show();}/** * 短時間顯示Toast *  * @param context * @param message */public static void showShort(Context context, int message){if (isShow)Toast.makeText(context, message, Toast.LENGTH_SHORT).show();}/** * 長時間顯示Toast *  * @param context * @param message */public static void showLong(Context context, CharSequence message){if (isShow)Toast.makeText(context, message, Toast.LENGTH_LONG).show();}/** * 長時間顯示Toast *  * @param context * @param message */public static void showLong(Context context, int message){if (isShow)Toast.makeText(context, message, Toast.LENGTH_LONG).show();}/** * 自訂顯示Toast時間 *  * @param context * @param message * @param duration */public static void show(Context context, CharSequence message, int duration){if (isShow)Toast.makeText(context, message, duration).show();}/** * 自訂顯示Toast時間 *  * @param context * @param message * @param duration */public static void show(Context context, int message, int duration){if (isShow)Toast.makeText(context, message, duration).show();}}

也是非常簡單的一個封裝,能省則省了~~

3、SharedPreferences封裝類SPUtils

package com.zhy.utils;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Map;import android.content.Context;import android.content.SharedPreferences;public class SPUtils{/** * 儲存在手機裡面的檔案名稱 */public static final String FILE_NAME = "share_data";/** * 儲存資料的方法,我們需要拿到儲存資料的具體類型,然後根據類型調用不同的儲存方法 *  * @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);}/** * 得到儲存資料的方法,我們根據預設值得到儲存的資料的具體類型,然後調用相對於的方法擷取值 *  * @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;}/** * 移除某個key值已經對應的值 * @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);}/** * 清除所有資料 * @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);}/** * 查詢某個key是否已經存在 * @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);}/** * 返回所有的索引值對 *  * @param context * @return */public static Map<String, ?> getAll(Context context){SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);return sp.getAll();}/** * 建立一個解決SharedPreferencesCompat.apply方法的一個相容類 *  * @author zhy *  */private static class SharedPreferencesCompat{private static final Method sApplyMethod = findApplyMethod();/** * 反射尋找apply的方法 *  * @return */@SuppressWarnings({ "unchecked", "rawtypes" })private static Method findApplyMethod(){try{Class clz = SharedPreferences.Editor.class;return clz.getMethod("apply");} catch (NoSuchMethodException e){}return null;}/** * 如果找到則使用apply執行,否則使用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();}}}

對SharedPreference的使用做了建議的封裝,對外公布出put,get,remove,clear等等方法;

注意一點,裡面所有的commit操作使用了SharedPreferencesCompat.apply進行了替代,目的是儘可能的使用apply代替commit

首先說下為什麼,因為commit方法是同步的,並且我們很多時候的commit操作都是UI線程中,畢竟是IO操作,儘可能非同步;

所以我們使用apply進行替代,apply非同步進行寫入;

但是apply相當於commit來說是new API呢,為了更好的相容,我們做了適配;

SharedPreferencesCompat也可以給大家建立相容類提供了一定的參考~~


歡迎大家提意見~~可以不斷的改進這些類~



Android快速開發系列 常用工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.