Skip to content

Instantly share code, notes, and snippets.

@Sirelon
Last active August 29, 2015 14:27
Show Gist options
  • Save Sirelon/a7c368829c95c0a30db4 to your computer and use it in GitHub Desktop.
Save Sirelon/a7c368829c95c0a30db4 to your computer and use it in GitHub Desktop.
Simple class which wrapper into self SharedPrefernce object and work with it.
package com.sirelon;
import android.content.Context;
import android.content.SharedPreferences;
/**
* @author romanishin
* @since 21.08.15.
*/
public class AppSettings {
private static final String PREF_NAME = "{prefernceName}}";
private final SharedPreferences mPref;
public AppSettings(Context context) {
this.mPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public void putString(String key, String value) {
mPref.edit().putString(key, value).apply();
}
public void putInt(String key, int value) {
mPref.edit().putInt(key, value).apply();
}
public void putLong(String key, long value) {
mPref.edit().putLong(key, value).apply();
}
public boolean exist(String key) {
return mPref.contains(key);
}
public String getString(String key) {
return mPref.getString(key, "");
}
public int getInt(String key) {
return mPref.getInt(key, -1);
}
public long getLong(String key) {
return mPref.getLong(key, -1);
}
public void remove(String key){
mPref.edit().remove(key).apply();
}
public void clearAll() {
mPref.edit().clear().apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment