编写Singleton类来管理Android SharedPreferences

前端之家收集整理的这篇文章主要介绍了编写Singleton类来管理Android SharedPreferences前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试编写一个单例类来监督涉及共享首选项的所有操作.

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

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

stg_full_screen: true // as boolean

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

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;
    }

}

我的问题是:

>这是明智的做法吗?
>我如何有效地确定偏好的类型?

谢谢

解决方法

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

没有静态上下文引用,每个属性的静态getter / setter,在需要时,您可以为某些属性添加内存缓存值,以便从内存中更快地获取它,而不是从SharedPreferences中读取.明确的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
}
原文链接:https://www.f2er.com/android/314564.html

猜你在找的Android相关文章