Created
March 28, 2018 18:24
-
-
Save arnyigor/9f2b18cd767fdf99f3862ca90ef6f941 to your computer and use it in GitHub Desktop.
Класс работы с SharedPreferenses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.arny.arnylib.utils; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
import android.support.annotation.NonNull; | |
public class Config { | |
private static SharedPreferences settings = null; | |
private static SharedPreferences getSettings(@NonNull Context context) { | |
if(settings == null) { | |
settings = PreferenceManager.getDefaultSharedPreferences(context); | |
} | |
return settings; | |
} | |
/** | |
* Получение конфига по ключу | |
* @param key | |
* Ключ | |
* @param context | |
* Контекст | |
* @return | |
* Значение конфига | |
*/ | |
public static String getString(String key,@NonNull Context context) { | |
settings = getSettings(context); | |
return settings.getString(key, null); | |
} | |
/** | |
* Получение конфига по ключу | |
* @param key | |
* Ключ | |
* @param context | |
* Контекст | |
* @param defaultVal | |
* Значение по умолчанию | |
* @return | |
* Значение конфига | |
*/ | |
public static String getString(String key,@NonNull Context context,String defaultVal) { | |
settings = getSettings(context); | |
return settings.getString(key, defaultVal); | |
} | |
/** | |
* Получение конфига по ключу | |
* @param key | |
* Ключ | |
* @param context | |
* Контекст | |
* @return | |
* Значение конфига | |
*/ | |
public static Integer getInt(String key,@NonNull Context context) { | |
settings = getSettings(context); | |
return settings.getInt(key, 0); | |
} | |
public static Long getLong(String key,@NonNull Context context) { | |
settings = getSettings(context); | |
return settings.getLong(key, 0); | |
} | |
/** | |
* Получение конфига по ключу | |
* @param key | |
* Ключ | |
* @param context | |
* Контекст | |
* @return | |
* Значение конфига | |
*/ | |
public static boolean getBoolean(String key,boolean defaultVal,@NonNull Context context) { | |
settings = getSettings(context); | |
return settings.getBoolean(key, defaultVal); | |
} | |
/** | |
* Установка конфига | |
* @param key | |
* Ключ | |
* @param value | |
* Значение | |
* @param context | |
* Текущий контекст | |
*/ | |
public static void setString(String key, String value,@NonNull Context context) { | |
settings = getSettings(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putString(key, value); | |
editor.apply(); | |
} | |
/** | |
* Установка числового конфига | |
* @param key | |
* Ключ | |
* @param value | |
* Значение | |
* @param context | |
* Текущий контекст | |
*/ | |
public static void setBoolean(String key, boolean value,@NonNull Context context) { | |
settings = getSettings(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putBoolean(key, value); | |
editor.apply(); | |
} | |
/** | |
* Установка числового конфига | |
* @param key | |
* Ключ | |
* @param value | |
* Значение | |
* @param context | |
* Текущий контекст | |
*/ | |
public static void setInt(String key, Integer value,@NonNull Context context) { | |
settings = getSettings(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putInt(key, value); | |
editor.apply(); | |
} | |
public static void setLong(String key, long value,@NonNull Context context) { | |
settings = getSettings(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.putLong(key, value); | |
editor.apply(); | |
} | |
/** | |
* Удаление ключа из конфига | |
* @param key | |
* Ключ | |
* @param context | |
* Контекст | |
*/ | |
public static void remove(String key,@NonNull Context context) { | |
settings = getSettings(context); | |
SharedPreferences.Editor editor = settings.edit(); | |
editor.remove(key); | |
editor.apply(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment