Library that uses android preferences quickly to write and read int, float, long, String, boolean, Object from memory.
You can use this library anywhere on your app, it just needs a context.
public class PrefsApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
PreferencesManager pm = new PreferencesManager(this);
pm.setInt("intKey", 5);
pm.setFloat("floatKey", 2.392658f);
pm.setLong("longKey", 12000000000L);
pm.setString("stringKey", "Test string");
pm.setBoolean"boolKey", false);
pm.setObject("objectKey", new String("Test object"));
int preferencesInt = pm.getInt("intKey");
float preferencesFloat = pm.getFloat("floatKey");
long preferencesLong = pm.getLong("longKey");
String preferencesString = pm.getString("stringKey");
boolean preferencesBoolean = pm.getBoolean("boolKey");
String preferencesStringObject = (String)pm.getObject("objectKey");
}
}Installation is done using Jitpack. Add to the root application build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}In the single module build.gradle add
dependencies {
...
implementation 'it.lucacrema:android-preferences:3.1.1'
}Initialization
PreferencesManager pm = new PreferencesManager(context);With Android preferences you can read and write int, float, long, String, boolean and Object, the way you do it is:
pm.set{type of data}(key, value);
pm.get{type of data}(key);Where {type of data} can be Int, Float, Long, String, Boolean, Object
There is a method to get a Map<String, ?> of all saved values:
pm.getAllValues();You can also remove saved values of any type
pm.removeValue(key);And remove all the saved values
pm.removeAllValues();If you want to save a counter in preferences and update it you can use
pm.updateInt key); //Adds 1 to the currently saved value
pm.updateInt(key, 5); //Adds 5 to the currently saved valueIf you need to save a counters that resets to 1 once it gets to a defined value you can use:
pm.shiftInt(key, 3); //returns 1 (if the value was empty)
pm.shiftInt(key, 3); //returns 2
pm.shiftInt(key, 3); //returns 3
pm.shiftInt(key, 3); //returns 1If you were to use the same string key for two values of different type, the old value would be overwritten
pm.setInt("key", 5);
pm.setString("key", "string");
pm.getInt("key"); //Throws ClassCastException
pm.getString("key"); //returns "string"MIT License
Copyright (c) 2019 Luca Crema
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.