Last active
August 3, 2016 16:09
-
-
Save Ipzzer/6b51e651336ceab9b01f1e7900cdaf62 to your computer and use it in GitHub Desktop.
Android Shared Preferences Helper Class
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
import android.app.Activity; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
/** | |
* @author Israel Pérez | |
* 2013.05.08 | |
*/ | |
public class Preferences { | |
private static final String APP_SHARED_PREFS = "config"; | |
private SharedPreferences sharedPreferences; | |
private Context ctx; | |
public Preferences(Context context) { | |
this.ctx = context; | |
this.sharedPreferences = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE); | |
} | |
/** | |
* Obtiene el valor del parametro enviado si el tipo de Dato es String | |
* | |
* @param key | |
* @return valor del key | |
*/ | |
public String getDataString(String key) { | |
return sharedPreferences.getString(key, ""); | |
} | |
/*** | |
* Obtiene el valor del parametro enviado si el tipo de Dato es int | |
* | |
* @param key | |
* @return valor del key | |
*/ | |
public int getDataInt(String key) { | |
return sharedPreferences.getInt(key, 0); | |
} | |
/*** | |
* Obtiene el valor del parametro enviado si el tipo de Dato es long | |
* | |
* @param key | |
* @return valor del key | |
*/ | |
public long getDataLong(String key) { | |
return sharedPreferences.getLong(key, 0); | |
} | |
/** | |
* Almacena un registro en Preferencias.xml de tipo String | |
* | |
* @param key : Nombre con el que quiere llamar a la llave | |
* @param value: Valor que desea asignarle. Debe ser de tipo String | |
*/ | |
public void setDataString(String key, String value) { | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
editor.putString(key, value); | |
editor.commit(); | |
} | |
/** | |
* Almacena un registro en Preferencias.xml de tipo int | |
* | |
* @param key : Nombre con el que quiere llamar a la llave | |
* @param value: Valor que desea asignarle. Debe ser de tipo int | |
*/ | |
public void setDataInt(String key, int value) { | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
editor.putInt(key, value); | |
editor.commit(); | |
} | |
/** | |
* Almacena un registro en Preferencias.xml de tipo long | |
* | |
* @param key : Nombre con el que quiere llamar a la llave | |
* @param value: Valor que desea asignarle. Debe ser de tipo long | |
*/ | |
public void setDataLong(String key, long value) { | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
editor.putLong(key, value); | |
editor.commit(); | |
} | |
/** | |
* Borra un registro en Preferencias.xml de cualquier tipo de dato | |
* | |
* @param key : Nombre con el que quiere llamar a la llave | |
*/ | |
public void delete(String key) { | |
SharedPreferences.Editor editor = sharedPreferences.edit(); | |
editor.remove(key); | |
editor.commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use example:
Preferences pref = new Preferences(context);
pref.setDataInt("key", 1);
pref.setDataString("key", "value");