Android 框架:快速开发中Util常用工具类总结(1)

这篇博客汇总了Android开发中常用的工具类,包括SPUtils用于SharedPreferences的操作,DensityUtils处理屏幕密度转换,以及文件操作的相关辅助类如FileUtils,详细介绍了各个工具类的主要方法和使用场景,旨在提高开发效率。
摘要由CSDN通过智能技术生成

{

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也可以给大家创建兼容类提供了一定的参考~~

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 -1. 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, -1);

}

/**

  • 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 -1. 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, -1);

}

/**

  • 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 -1. 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, -1);

}

/**

  • 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.get

  • 26
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值