Android项目开发常用工具类SharedPreferences

Posted 谷哥的小弟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android项目开发常用工具类SharedPreferences相关的知识,希望对你有一定的参考价值。


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

SharedPreferencesUtils概述

SharedPreferencesUtils用于在SharedPreferences中进行数据存储操作。

SharedPreferencesUtils截图

SharedPreferencesUtils截图如下:

SharedPreferencesUtils源码

SharedPreferencesUtils源码如下:

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SharedPreferences工具类
 * 本文作者:谷哥的小弟
 * 博客地址:http://blog.csdn.net/lfdfhl
 */

public class SharedPreferencesUtils 
    private static final String PREFERENCE_NAME = "sp_name";
    private SharedPreferencesUtils() 

    /**
     * 获取SharedPreferences对象
     *
     * @param context 上下文
     * @return SharedPreferences对象
     */
    public static SharedPreferences getSharedPreferences(Context context) 
        SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences;
    

    /**
     * 获取Editor对象
     *
     * @param context 上下文
     * @return Editor对象
     */
    public static SharedPreferences.Editor getEditor(Context context) 
        SharedPreferences sharedPreferences = getSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        return editor;
    


    /**
     * 将文本数据存入本地
     *
     * @param context 上下文
     * @param key     key值
     * @param value   存入本地的字符串
     */
    public static void putString(Context context, String key, String value) 
        SharedPreferences.Editor editor = getEditor(context);
        editor.putString(key, value);
        editor.apply();
    

    /**
     * 通过key获取SharedPreferences指定的字符串
     *
     * @param context 上下文
     * @param key     key值
     * @return 本地存储的字符串
     * @see #getString(Context, String, String)
     */
    public static String getString(Context context, String key) 
        return getString(context, key, "");
    

    /**
     * 通过key获取SharedPreferences指定的字符串
     *
     * @param context      上下文
     * @param key          key值
     * @param defaultValue 默认的返回值
     * @return 本地存储的字符串或默认值
     */
    public static String getString(Context context, String key, String defaultValue) 
        SharedPreferences sharedPreferences = getSharedPreferences(context);
        return sharedPreferences.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 void putInt(Context context, String key, int value) 
        SharedPreferences.Editor editor = getEditor(context);
        editor.putInt(key, value);
        editor.apply();
    

    /**
     * get int preferences
     *
     * @param context 上下文
     * @param key     key值
     * @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      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 sharedPreferences = getSharedPreferences(context);
        return sharedPreferences.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 void putLong(Context context, String key, long value) 
        SharedPreferences.Editor editor = getEditor(context);
        editor.putLong(key, value);
        editor.apply();
    

    /**
     * get long preferences
     *
     * @param context 上下文
     * @param key     key值
     * @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      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 sharedPreferences = getSharedPreferences(context);
        return sharedPreferences.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.Editor editor = getEditor(context);
        editor.putFloat(key, value);
        return editor.commit();
    

    /**
     * get float preferences
     *
     * @param context 上下文
     * @param key     key值
     * @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      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 sharedPreferences = getSharedPreferences(context);
        return sharedPreferences.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.Editor editor = getEditor(context);
        return editor.commit();
    

    /**
     * get boolean preferences, default is false
     *
     * @param context 上下文
     * @param key     key值
     * @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      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 sharedPreferences = getSharedPreferences(context);
        return sharedPreferences.getBoolean(key, defaultValue);
    

    /**
     * 清空SharedPreferences缓存
     * @param context 上下文
     */
    public static void clear(Context context) 
        SharedPreferences.Editor editor = getEditor(context);
        editor.clear();
        editor.apply();
    



以上是关于Android项目开发常用工具类SharedPreferences的主要内容,如果未能解决你的问题,请参考以下文章

Android项目开发常用工具类BaseActivity

Android开发常用工具类

android开发Tost工具类管理

Android快速开发系列 10个常用工具类

我应该在我的应用程序中使用 Android SharedPreferences 还是 SQLite 来实现持久性?

android开发SD卡工具类