Instantly share code, notes, and snippets.
Last active
March 17, 2016 21:25
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save gaspardzul/c2c3b5272f2d38161643 to your computer and use it in GitHub Desktop.
Esta clase permite el uso de manera fácil de las preferencias compartidas de Android (SharePreferences).
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.demo.utils; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
/** | |
* Esta clase permite el manejo simple de SharedPreferences | |
* Created by gaspar on 02/12/15. | |
*/ | |
public class SharedPreferencesUtil{ | |
Context context; | |
public SharedPreferencesUtil(Context ctx){ | |
this.context = ctx; | |
} | |
/** | |
* Permite almacenar preferencias de tipo String | |
* @param key | |
* @param data | |
*/ | |
public void saveStrSharedPreference(String key,String data){ | |
SharedPreferences sharedPref =PreferenceManager.getDefaultSharedPreferences(context); | |
SharedPreferences.Editor editor = sharedPref.edit(); | |
editor.putString(key, data); | |
editor.apply(); | |
} | |
/** | |
* Permite obtener las preferencias de tipo String | |
* @param key | |
* @return | |
*/ | |
public String getStringSharedPreference(String key){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | |
return sharedPref.getString(key, ""); | |
} | |
/** | |
* Permite eliminar todas las preferencias del sistema | |
*/ | |
public void deleteAllSharedPreference(){ | |
SharedPreferences sharedPref =PreferenceManager.getDefaultSharedPreferences(context); | |
sharedPref.edit().clear().apply(); | |
} | |
/** | |
* Permite almacenar preferencias de tipo booleano (TRUE/FALSE) | |
* @param key | |
* @param data | |
*/ | |
public void saveBooleanSharedPreference(String key,boolean data){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | |
SharedPreferences.Editor editor = sharedPref.edit(); | |
editor.putBoolean(key, data); | |
editor.apply(); | |
} | |
/** | |
* Permite obtener una preferencia de tipo booleano (TRUE/FALSE) | |
* @param key | |
* @return | |
*/ | |
public boolean getBooleanSharedPreference(String key){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | |
return sharedPref.getBoolean(key, false); | |
} | |
/** | |
* Permite almacenar preferencias de tipo flotante | |
* @param key | |
* @param data | |
*/ | |
public void saveFloatSharedPreference(String key,float data){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | |
SharedPreferences.Editor editor = sharedPref.edit(); | |
editor.putFloat(key, data); | |
editor.apply(); | |
} | |
/** | |
* Permite obtener una preferencia de tipo flotante | |
* @param key | |
* @return | |
*/ | |
public float getFloatSharedPreference(String key){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | |
return sharedPref.getFloat(key, -1); | |
} | |
/** | |
* Permite almacenar preferencias de tipo entero | |
* @param key | |
* @param data | |
*/ | |
public void saveIntSharedPreference(String key,int data){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | |
SharedPreferences.Editor editor = sharedPref.edit(); | |
editor.putInt(key, data); | |
editor.apply(); | |
} | |
/** | |
* Permite obtener una preferencia de tipo entero | |
* @param key | |
* @return | |
*/ | |
public int getIntSharedPreference(String key){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | |
return sharedPref.getInt(key, -1); | |
} | |
/** | |
* Permite almacenar preferencias de tipo long | |
* @param key | |
* @param data | |
*/ | |
public void saveLongSharedPreference(String key,int data){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | |
SharedPreferences.Editor editor = sharedPref.edit(); | |
editor.putLong(key, data); | |
editor.apply(); | |
} | |
/** | |
* Permite obtener una preferencia de tipo long | |
* @param key | |
* @return | |
*/ | |
public long getLongSharedPreference(String key){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | |
return sharedPref.getLong(key, -1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment