grenderg / prefs Goto Github PK
View Code? Open in Web Editor NEWSimple Android SharedPreferences wrapper.
License: GNU General Public License v3.0
Simple Android SharedPreferences wrapper.
License: GNU General Public License v3.0
Hi,
I used below code for shared prefs. My friend suggested not use static methods. He advised me to use the class like you have implemented. Can you please suggest with which way we can improve performance.
public static void saveStringPref(Context context, String key, String value) {
SharedPreferences sharedPref = context.getSharedPreferences(
"Pref-Values", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(key, value);
editor.commit();
}
public static String getStringPref(Context context, String key,
String defaultValue) {
SharedPreferences sharedPref = context.getSharedPreferences(
"Pref-Values", Context.MODE_PRIVATE);
return sharedPref.getString(key, defaultValue);
}
public static void saveIntPref(Context context, String key, int value) {
SharedPreferences sharedPref = context.getSharedPreferences(
"Pref-Values", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(key, value);
editor.commit();
}
public static int getIntPerf(Context context, String key,
int defaultValue) {
SharedPreferences sharedPref = context.getSharedPreferences(
"Pref-Values", Context.MODE_PRIVATE);
return sharedPref.getInt(key, defaultValue);
}
public static void saveBooleanPref(Context context, String key, boolean value) {
SharedPreferences sharedPref = context.getSharedPreferences(
"Pref-Values", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(key, value);
editor.commit();
}
public static boolean getBooleanPerf(Context context, String key,
boolean defaultValue) {
SharedPreferences sharedPref = context.getSharedPreferences(
"Pref-Values", Context.MODE_PRIVATE);
return sharedPref.getBoolean(key, defaultValue);
}
public static void deletePref(Context context, String key) {
SharedPreferences sharedPref = context.getSharedPreferences(
"Pref-Values", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.remove(key);
editor.commit();
}
I notice by default this library handles preferences with the name 'preferences' but I think it would be cool to specify the name of the Shared Preference file the user wants to access.
Basically creating a call like:
Prefs.with(yourContext, "FILE NAME").readInt(YOUR_KEY_VALUE);
having the second parameter optional, defaulting to the what the library does now.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.