编写Singleton类以管理Android SharedPreferences [英] Writing Singleton Class To Manage Android SharedPreferences

查看:71
本文介绍了编写Singleton类以管理Android SharedPreferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个单例类来监督所有涉及共享首选项的操作.

i am trying to write a singleton class to oversee all operations involving shared preferences.

我有3个首选项文件,即常规文件,设置文件和临时文件

I have 3 preference files, general, settings, and temp

我希望能够使用此类编写给定类型的首选项,例如:

I want to be able to use this class to write a preference of a given type, for example:

stg_full_screen: true // as boolean

这是我到目前为止所做的:

This is what i have done so far:

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPrefManager extends Activity {

    // Globals
    private int GENERAL             = 1000;
    private int SETTINGS            = 2000;
    private int TEMP_STORE          = 3000;

    private String PREF_GENERAL     = "com.example.general";
    private String PREF_SETTINGS    = "com.example.settings";
    private String PREF_TEMP_STORE  = "com.example.temp_store";


    private SharedPreferences general;
    private SharedPreferences settings;
    private SharedPreferences tempStore;

    private SharedPreferences.Editor general_editor;
    private SharedPreferences.Editor settings_editor;
    private SharedPreferences.Editor temp_store_editor;





    // Instantiate singleton object
    private static SharedPrefManager ourInstance = new SharedPrefManager();


    public static SharedPrefManager getInstance() { return ourInstance; }

    private SharedPrefManager() {
        // Get handle on all preference files
        general   = getSharedPreferences(PREF_GENERAL, Context.MODE_PRIVATE);
        settings  = getSharedPreferences(PREF_SETTINGS, Context.MODE_PRIVATE);
        tempStore = getSharedPreferences(PREF_TEMP_STORE, Context.MODE_PRIVATE);

        // provision editors for all preference files
        general_editor    = general.edit();
        settings_editor   = settings.edit();
        temp_store_editor = tempStore.edit();
    }



    private String read_prefs (String pref_name) {
        // this method reads a preference and returns it
        // ideally, i would want to be able to return appropriate types by request
        // e.g boolean, string
        return null;
    }

    private void write_prefs (String pref_name, String pref_val) {
        // this method would take a preference and write the appropriate type to prefs
    }


    // this method determines where to put a preference by checking the name of the key
    // this works because i use the following naming conventions
    // stg_name for settings, tmp_name for all that goes into tempStore

    private String resolve_pref_category (String path) {
        if (path.startsWith("stn"))         return PREF_SETTINGS;
        else if (path.startsWith("tmp"))    return PREF_TEMP_STORE;
        else                                return PREF_GENERAL;
    }

}

我的问题是:

  1. 这是明智之举吗?
  2. 如何有效确定偏好类型?

谢谢

推荐答案

通常,我使用类似这样的东西:

Usually, I use something like this:

无静态Context引用,每个属性的静态getter/setter,在需要时可以为某些属性添加内存缓存值,以从内存中更快地获取它,而不是从SharedPreferences中读取.清除api.

No static Context reference, static getter/setter for each property, when required you can add memory cached value for some property to get it faster from memory instead of reading from SharedPreferences. Clear api.

public class SharedPreferencesManager {

    private static final String APP_SETTINGS = "APP_SETTINGS";


    // properties
    private static final String SOME_STRING_VALUE = "SOME_STRING_VALUE";
    // other properties...


    private SharedPreferencesManager() {}

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
    }

    public static String getSomeStringValue(Context context) {
        return getSharedPreferences(context).getString(SOME_STRING_VALUE , null);
    }

    public static void setSomeStringValue(Context context, String newValue) {
        final SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putString(SOME_STRING_VALUE , newValue);
        editor.commit();
    }

    // other getters/setters
}

这篇关于编写Singleton类以管理Android SharedPreferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆