Skip to content

Repository files navigation

SimplePreferences

Android library for helping simplify the preferences access. You can also implement your PreferenceStorage to save the preference into database or encrypt it.

Download

Grab via Maven:

 <dependency>
<groupId>com.ioenv</groupId>
<artifactId>preferences-library</artifactId>
<version>1.0</version>
</dependency>

or Gradle:

 compile 'com.ioenv:preferences-library:1.0'

Uasge

initialization

Initialize with default settings

Preferences.init(getContext());

Initialize with an encoded preferences storage

Preferences.init(newEncodedPreferenceStorage(getContext()));

get/set simple preference

get/set by Preferences.set(KEY, VALUE) and Preferences.get(KEY, DEFAULT_VALUE)

Preferences.set("str_config", "String");
Preferences.set("int_config", 1234);
Preferences.set("long_config", 1234567890L);
Preferences.set("bool_config", true);
Preferences.set("float_config", 123.45);
Preferences.get("str_config""Default");
Preferences.get("int_config"0);
Preferences.get("long_config"0L);
Preferences.get("float_config"0f);
Preferences.get("bool_config"false);

get/set object preference

classDataObject {
longtimestamp;
Stringmessage;
publicDataObject(longtimestamp, Stringmessage) {
this.timestamp = timestamp;
this.message = message;
}
}
Preferences.set("obj_config", newDataObject(1, "Hi"));
DataObjectdata = Preferences.get("obj_config"DataObject.class, null);

get/set list preference

string list

Preferences.setStrings("str_list", newArrayList<String>() {{
add("String 1");
add("String 2");
add("String 3");
}};
List<String> result = Preferences.getStrings("str_list");

object list

List<DataObject> objList = newArrayList<DataObject>() {
objList.add(newDataObject(1, "A"));
objList.add(newDataObject(2, "B"));
objList.add(newDataObject(3, "C"));
Preferences.set("obj_list", Preferences.toJson(objList));
List<DataObject> anotherList = newArrayList<DataObject>() {
{
add(newDataObject(1, "A"));
add(newDataObject(2, "B"));
add(newDataObject(3, "C"));
}
};
Preferences.set("another_list", Preferences.toJson(newArrayList<DataObject>(anotherList)));
List<DataObject> result = Preferences.getList("obj_list", newTypeToken<ArrayList<DataObject>>() {}.getType());

FAQ

For any questions, please submit to issues.

Developed By

License

Copyright 2015 Jinyu Liu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

SimplePreferences is a Android library to help simplify the preferences access.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages