apache_conf SharedPreference设置类封装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache_conf SharedPreference设置类封装相关的知识,希望对你有一定的参考价值。

public class Configs {

    private static final String sName = "settings";

    public enum Key {
        // 这里放置设置项的 Key
    }

    public static Set<String> getSetting(Key key, Set<String> defaultValue) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        return preferences.getStringSet(String.valueOf(key.ordinal()), defaultValue);
    }

    public static void putSetting(Key key, boolean value) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(String.valueOf(key.ordinal()), value);
        editor.apply();
    }

    public static void putSetting(Key key, int value) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(String.valueOf(key.ordinal()), value);
        editor.apply();
    }

    public static void putSetting(Key key, long value) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(String.valueOf(key.ordinal()), value);
        editor.apply();
    }

    public static void putSetting(Key key, float value) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putFloat(String.valueOf(key.ordinal()), value);
        editor.apply();
    }

    public static void putSetting(Key key, String value) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(String.valueOf(key.ordinal()), value);
        editor.apply();
    }

    public static void putSetting(Key key, Set<String> value) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putStringSet(String.valueOf(key.ordinal()), value);
        editor.apply();
    }

    public static boolean getSetting(Key key, boolean defaultValue) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        return preferences.getBoolean(String.valueOf(key.ordinal()), defaultValue);
    }

    public static int getSetting(Key key, int defaultValue) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        return preferences.getInt(String.valueOf(key.ordinal()), defaultValue);
    }

    public static long getSetting(Key key, long defaultValue) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        return preferences.getLong(String.valueOf(key.ordinal()), defaultValue);
    }

    public static float getSetting(Key key, float defaultValue) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        return preferences.getFloat(String.valueOf(key.ordinal()), defaultValue);
    }

    public static String getSetting(Key key, String defaultValue) {
        SharedPreferences preferences = App.getInstance().getSharedPreferences(sName, Context.MODE_PRIVATE);
        return preferences.getString(String.valueOf(key.ordinal()), defaultValue);
    }
}

以上是关于apache_conf SharedPreference设置类封装的主要内容,如果未能解决你的问题,请参考以下文章

实现用户选择主题

使用SharedPreferences进行数据存储

apache_conf Redireciones

apache_conf Seguridad .htaccess

apache_conf htaccess的

apache_conf 基本认证