Get default value defined in preferences.xml - java

I want to retrieve the default value from the myPreferences.xml with the AndroidPreferenceManager. I got it to work with int and strings, but how to load the default value from a Boolean?
Snippet from helperMethods:
public static void putPref(String key, String value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String getPref(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
public static Boolean getPrefBoolean(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(key, null);
}
Snippet from myPreferences:
<EditTextPreference
app:defaultValue="#string/value_content_id"
app:key="#string/key_device_id"
app:summary="#string/device_id"
app:title="#string/title_content_id" />
<Preference
app:key="key_wifi"
app:summary="WifiSettings"
app:title="Set Wifi" />
<CheckBoxPreference
app:defaultValue="true"
app:key="#string/key_app_lock"
app:summary="#string/summary_app_lock"
app:title="#string/title_app_lock" />
the goal is to get the default value from the myPreference.xml when no value has been set yet. It works with Strings, and Integers but how with an boolean? Is there a workaround?Sometimes the default might be a string, int or boolean. My problem right now is, that SharedPreferences.getBoolean doesnt accept a null value as parameter. What I really don't want to do is defining the default value when using get. Since it's already declared in the preference.xml.

I am not entirely certain what you are asking. But if you check for the presence of the preference using the contains method on the SharedPreferences, you can determine if you would like to return null instead inside of your helper method.
https://developer.android.com/reference/android/content/SharedPreferences.html#contains(java.lang.String)

Related

LogIn & SignIn with Retrofit

I created a simple localhost API with a JSON database on my computer. I already implemented functions where hosts can create a new profile or log in to existing one.
"hosts": [
{
"id": 1,
"login": "drey95music",
"password": "8526000Qq",
"email": "shtandrinity#gmail.com",
"eventName": "FDC Open Mic",
"latitude": 41.924676,
"longitude": -87.700266
}],
"artists": [
{
"id": 1,
"name": "MDQ",
"phone": 7738375513,
"isLocated": "true",
"userId": 1
}]
My question is what is the best way to store/remember/check if a user is logged in so he/she would have more functionality?
Thank you.
Actually you can have the implementation in a lot of different ways based on the use case that you have. Personally, I would feel, if the user information is something very small or you just have to store a token in order to pass it to the subsequent API requests, then storing that token in the SharedPreferences might be a good idea. So that this can be accessed from anywhere in the app, and also this storage is persistent - that is, even if you are closing the application and reopen it, it says in the persistent memory.
That being said, if you have to store a lot of information, I would recommend having those stored in the SQLite database. SharedPreferences is not recommended for storing large amounts of data.
I hope that helps!
Usually developer use Shared Preference to store some value behave of specific key. I've made a generic shared preference class and use it every android project without any hesitation.
public class SaveInSharedPreference {
public static final String IS_LOGIN = "isLogin";
private final SharedPreferences pref;
private final SharedPreferences.Editor editor;
// shared pref mode
private final int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "PREF_NAME";
#SuppressLint("CommitPrefEdits")
public SaveInSharedPreference(Context context) {
pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setBoolean(String key, boolean value){
editor.putBoolean(key, value);
editor.commit();
}
public boolean getBoolean(String key) {
return pref.getBoolean(key, false);
}
public void setString(String key, String value) {
editor.putString(key, value);
editor.commit();
}
public String getString(String key) {
return pref.getString(key, "");
}
public void setInteger(String key, int value) {
editor.putInt(key, value);
editor.commit();
}
public Integer getInteger(String key) {
return pref.getInt(key, 0);
}
public void clearSavedInSharedPreference(){
editor.clear();
editor.commit();
}
}
After successfully authenticate a user use this line to add/update value of a key by:
new SaveInSharedPreference(this).setBoolean(IS_LOGIN, true);
And you can get the value of key by:
new SaveInSharedPreference(this).getBoolean(IS_LOGIN);
Shared preference stored only String, Boolean, Long, Float, Integer. If you want to store JsonObject in shared preference you can do it as well. But you have to change your JsonObject into string then store it and get value from shared preference in string then conert it into your JsonObject given below:
Store/Update data
new SaveInSharedPreference(this).setString("user_data", new Gson().toJson(put_your_model_here));
Get data
new Gson().fromJson(new SaveInSharedPreference(this).getString("user_data"));
You can also store your list in shared preference.

Gson deserialize the boolean JSON only with false, regardless to input

My object consists of five fields :
public class ConfigurationItem {
#SerializedName("show_interest")
boolean show_interest;
#SerializedName("bid_with_price")
boolean bid_with_price;
#SerializedName("anonymous_orders")
boolean anonymous_orders;
#SerializedName("orders_progress_status")
boolean orders_progress_status;
#SerializedName("orders_progress_messages")
boolean orders_progress_messages;
}
I parse this items from the web server and receive string like this :
{
"ordersProgressStatus":true,
"showInterest":false,
"anonymousOrders":true,
"bidWithPrice":true,
"ordersProgressMessages":true
}
I receive the JSON and save it to the SharedPreferences like this :
public static void saveCurrentConfiguration(Context mContext, JSONObject jsonObject ) {
SharedPreferences sharedPreferences = mContext.getApplicationContext().getSharedPreferences(Constants.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = sharedPreferences.edit();
prefsEditor.putString(Constants.SHARED_CURRENT_CONFIG, jsonObject.toString());
prefsEditor.apply();
}
But when I want to read the saved object :
public static ConfigurationItem getCurrentConfiguration(Context mContext)
{
SharedPreferences sharedPreferences = mContext.getApplicationContext().getSharedPreferences(Constants.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString(Constants.SHARED_CURRENT_CONFIG, null);
ConfigurationItem configurationItem = gson.fromJson(json, ConfigurationItem.class);
Log.i(TAG + " loaded config", configurationItem.toString());
return configurationItem;
}
in the configurationItem I get only false values. Besides, the read string from the SharedPreference is correct, but when I use Gson for deserialize, the object is filled with wrong values.
What can be the solution ?
When using the annotation #SerializedName this references the key value in the JSON string. So instead of doing #SerializedName("show_interest") to serialize the value from "showInterest" do #SerializedName("showInterest")
Using serialized name is handy when you don't want to tie the name of a JSON key to name of a field. For instance when you prefer to use the JAVA standaard convention of prefixing private field with m like private boolean mShowInterest; so when later you refactor the field name to something else you can do a easy refactor, or if the JSON key changes you on have to change the annotation.

Android preferences not working

I'm new to the amazing world of java, but my Obj-C experince tells me that this code:
Preferences prefs = Preferences.userRoot();
String tickets = prefs.get("tickets", "???");
Log.d("Prefs", tickets);
prefs.put("tickets", "!!!");
should produce ??? on the first run, and !!! on the second run. But it does not. I see ??? all the time.
What am I missing? Maybe some permissions? Maybe calling some save() method on prefs object?
I've also tried:
Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
No luck either.
I think you're probably looking for SharedPreferences... "context" is your Activity or Application
try:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String tickets = prefs.getString("tickets", "???");
Log.d("Prefs", tickets);
prefs.edit().putString("tickets", "!!!").commit();
Seems like you want to use SharedPreferences to store some data. In order to write to preferences try this function:
public void putPref(Context context, String key, String value){
SharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = SharedPrefs.edit();
editor.putString(key, value);
editor.commit();
}
If you want to put the value "???" using key "tickets" use:
putPref(context, "tickets", "???");
You can retrieve that value by using:
SharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
If you want to get value for the key "ticket" use:
SharedPrefs.getString("tickets", "default");
where "default" is the default value if the preference isn't found. Hope this helps.
You can save the values to preference using the below function
public static void saveTickets(String customerId,Context context)
{
SharedPreferences preferences;
preferences = context.getSharedPreferences("sample",Context.MODE_PRIVATE);
preferences.edit().putString("tickets", customerId).commit();
}
Get the values from preference using the below function. The parameter you need to pass is just context
public static String getTickets(Context context)
{
SharedPreferences preferences;
preferences = context.getSharedPreferences("sample",Context.MODE_PRIVATE);
String customerId = preferences.getString("tickets", "");
return customerId;
}

Shared preferences between classes (static)?

I have two classes, one loggedIn, and a User class. In the loggedIn class I want to show the shared preferences that I made when the user logs in.
loginPrefs = getSharedPreferences("loginpreferences",Context.MODE_PRIVATE);
SharedPreferences.Editor loginEditor = loginPrefs.edit();
loginEditor.putString("userID", userIDCrypt);
loginEditor.commit();
Now i want to make in the user class a getID() method, that I can call the method from every class with User.getID();. How can I do this?
I need the userID in multiple classes, so I want one activity (called getID e.g.) that gives me the user ID.
try this in one Activity :
SharedPreferences sp;
SharedPreferences.Editor edit;
sp = getSharedPreferences("enter", MODE_PRIVATE);
edit = sp.edit();
edit.putString("name", username);
edit.putString("pwd", password);
edit.commit();
in next activity :
SharedPreferences sp = getSharedPreferences("enter", MODE_PRIVATE);
sp.getString("name", "default value"));
sp.getString("pwd", "default value"));
do like this make one class for your sharedpreference
public class Session {
private SharedPreferences prefs;
public Session(Context cntx) {
// TODO Auto-generated constructor stub
prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
}
public void setusename(String usename) {
prefs.edit().putString("usename", usename).commit();
prefsCommit();
}
public String getusename() {
String usename = prefs.getString("usename","");
return usename;
}
}
now after making this class when u want to use this use like this
make object og this class like
private Session session;//global variable
session = new Session(cntx); //in oncreate
and now we set sharedpreference then use this like
session.setusename("USERNAME");
now when ever u want to get username then same work for session object and call this
session.getusename();
best of luck :) same for password
Sorry if there is any syntax or compilation issues, just typing out of my head, and might make some mistakes. So in essence this is an approach that you can use to expose your prefs from one simple class and access it from any where.
public class MySharedPrefs {
public SharedPreferences sp;
public MySharedPrefs()
{
this.sp = c.getSharedPreferences("prefs", Context.MODE_PRIVATE);
}
public static String getStringFieldValue(Context c, String fieldName)
{
MySharedPrefs p = new MySharedPrefs(c);
return p.sp.getString(fieldName, "default value");
}
public static void setStringValue(Context c, String fieldName, String value)
{
MySharedPrefs p = new MySharedPrefs(c);
SharedPreferences.Editor edit;
edit = p.sp.edit();
edit.putString(fieldName, value);
edit.commit();
}
}
Then use it like this in your activity:
MySharedPrefs.getStringFieldValue(this, "name");
You can the also expand on this class and add additional helper methods such as a getUserName or etc.
UPDATE:
When calling this from another static class, that class needs to have a reference to your applications context, you then need to provide that context to this function instead of using this.
MySharedPrefs.getStringFieldValue(context, "name"); //if your other static class has a property called context that contains your applications context

Android SharedPreferences Best Practices

In an application I have been building we rely on SharedPreferences quite a bit, this got me thinking about what is best practice when it comes to accessing SharedPreferences. For instance many people say the appropriate way to access it is via this call:
PreferenceManager.getDefaultSharedPreferences(Context context)
However it seems like this could be dangerous. If you have a large application that is relying on SharedPreferences you could have key duplication, especially in the case of using some third party library that relies on SharedPreferences as well. It seems to me that the better call to use would be:
Context.getSharedPreferences(String name, int mode)
This way if you have a class that heavily relies on SharedPreferences you can create a preference file that is used only by your class. You could use the fully qualified name of the class to ensure that the file will most likely not be duplicated by someone else.
Also based on this SO question: Should accessing SharedPreferences be done off the UI Thread?, it seems that accesses SharedPreferences should be done off the UI thread which makes sense.
Are there any other best practices Android developers should be aware of when using SharedPreferences in their applications?
I've wrote a little article that can also be found here. It describes what SharedPreferences is :
Best Practice: SharedPreferences
Android provides many ways of storing application data. One of those ways leads us to the SharedPreferences object which is used to store private primitive data in key-value pairs.
All logic are based only on three simple classes:
SharedPreferences
SharedPreferences.Editor
SharedPreferences.OnSharedPreferenceChangeListener
SharedPreferences
SharedPreferences is main of them. It's responsible for getting (parsing) stored data, provides interface for getting Editor object and interfaces for adding and removing OnSharedPreferenceChangeListener
To create SharedPreferences you will need Context object (can be an application Context)
getSharedPreferences method parses Preference file and creates Map object for it
You can create it in few modes provided by Context. You should always use MODE_PRIVATE, as all the other modes are deprecated since API level 17.
// parse Preference file
SharedPreferences preferences = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// get values from Map
preferences.getBoolean("key", defaultValue)
preferences.get..("key", defaultValue)
// you can get all Map but be careful you must not modify the collection returned by this
// method, or alter any of its contents.
Map<String, ?> all = preferences.getAll();
// get Editor object
SharedPreferences.Editor editor = preferences.edit();
//add on Change Listener
preferences.registerOnSharedPreferenceChangeListener(mListener);
//remove on Change Listener
preferences.unregisterOnSharedPreferenceChangeListener(mListener);
// listener example
SharedPreferences.OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener
= new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
}
};
Editor
SharedPreferences.Editor is an Interface used for modifying values in a SharedPreferences object. All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()
Use simple interface to put values in Editor
Save values synchronous with commit() or asynchronous with apply which is faster. In fact of using different threads using commit() is safer. Thats why I prefer to use commit().
Remove single value with remove() or clear all values with clear()
// get Editor object
SharedPreferences.Editor editor = preferences.edit();
// put values in editor
editor.putBoolean("key", value);
editor.put..("key", value);
// remove single value by key
editor.remove("key");
// remove all values
editor.clear();
// commit your putted values to the SharedPreferences object synchronously
// returns true if success
boolean result = editor.commit();
// do the same as commit() but asynchronously (faster but not safely)
// returns nothing
editor.apply();
Performance & Tips
SharedPreferences is a Singleton object so you can easily get as many references as you want, it opens file only when you call getSharedPreferences first time, or create only one reference for it.
// There are 1000 String values in preferences
SharedPreferences first = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 4 milliseconds
SharedPreferences second = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 0 milliseconds
SharedPreferences third = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
// call time = 0 milliseconds
As SharedPreferences is a Singleton object you can change any of It's instances and not be scared that their data will be different
first.edit().putInt("key",15).commit();
int firstValue = first.getInt("key",0)); // firstValue is 15
int secondValue = second.getInt("key",0)); // secondValue is also 15
Remember the larger the Preference object is the longer get, commit, apply, remove and clear operations will be. So it's highly recommended to separate your data in different small objects.
Your Preferences will not be removed after Application update. So there are cases when you need to create some migration scheme. For example you have Application that parse local JSON in start of application, to do this only after first start you decided to save boolean flag wasLocalDataLoaded. After some time you updated that JSON and released new application version. Users will update their applications but they will not load new JSON because they already done it in first application version.
public class MigrationManager {
private final static String KEY_PREFERENCES_VERSION = "key_preferences_version";
private final static int PREFERENCES_VERSION = 2;
public static void migrate(Context context) {
SharedPreferences preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
checkPreferences(preferences);
}
private static void checkPreferences(SharedPreferences thePreferences) {
final double oldVersion = thePreferences.getInt(KEY_PREFERENCES_VERSION, 1);
if (oldVersion < PREFERENCES_VERSION) {
final SharedPreferences.Editor edit = thePreferences.edit();
edit.clear();
edit.putInt(KEY_PREFERENCES_VERSION, currentVersion);
edit.commit();
}
}
}
SharedPreferences are stored in an xml file in the app data folder
// yours preferences
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml
// default preferences
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
Android guide.
Sample Code
public class PreferencesManager {
private static final String PREF_NAME = "com.example.app.PREF_NAME";
private static final String KEY_VALUE = "com.example.app.KEY_VALUE";
private static PreferencesManager sInstance;
private final SharedPreferences mPref;
private PreferencesManager(Context context) {
mPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
public static synchronized void initializeInstance(Context context) {
if (sInstance == null) {
sInstance = new PreferencesManager(context);
}
}
public static synchronized PreferencesManager getInstance() {
if (sInstance == null) {
throw new IllegalStateException(PreferencesManager.class.getSimpleName() +
" is not initialized, call initializeInstance(..) method first.");
}
return sInstance;
}
public void setValue(long value) {
mPref.edit()
.putLong(KEY_VALUE, value)
.commit();
}
public long getValue() {
return mPref.getLong(KEY_VALUE, 0);
}
public void remove(String key) {
mPref.edit()
.remove(key)
.commit();
}
public boolean clear() {
return mPref.edit()
.clear()
.commit();
}
}
If you have a large application that is relying on SharedPreferences you could have key duplication, especially in the case of using some third party library that relies on SharedPreferences as well.
Libraries should not use that particular SharedPreferences. The default SharedPreferences should only be used by the application.
This way if you have a class that heavily relies on SharedPreferences you can create a preference file that is used only by your class.
You are certainly welcome to do this. I wouldn't, at the application level, as the primary reason for SharedPreferences is to have them be shared among the components in the application. A development team should have no problem managing this namespace, just as they should have no problem managing names of classes, packages, resources, or other project-level stuff. Moreover, the default SharedPreferences are what your PreferenceActivity will use.
However, going back to your libraries point, reusable libraries should use a separate SharedPreferences for their library only. I would not base it on a class name, because then you are one refactoring away from breaking your app. Instead, pick a name that is unique (e.g., based on the library name, such as "com.commonsware.cwac.wakeful.WakefulIntentService") but stable.
it seems that accesses SharedPreferences should be done off the UI thread which makes sense.
Ideally, yes. I recently released a SharedPreferencesLoader that helps with this.
Are there any other best practices Android developers should be aware of when using SharedPreferences in their applications?
Don't over-rely upon them. They are stored in XML files and are not transactional. A database should be your primary data store, particularly for data you really don't want to lose.
In kotlin, use of SharedPreferences can be simplified in the following way.
class Prefs(context: Context) {
companion object {
private const val PREFS_FILENAME = "app_prefs"
private const val KEY_MY_STRING = "my_string"
private const val KEY_MY_BOOLEAN = "my_boolean"
private const val KEY_MY_ARRAY = "string_array"
}
private val sharedPrefs: SharedPreferences =
context.getSharedPreferences(PREFS_FILENAME, Context.MODE_PRIVATE)
var myString: String
get() = sharedPrefs.getString(KEY_MY_STRING, "") ?: ""
set(value) = sharedPrefs.edit { putString(KEY_MY_STRING, value) }
var myBoolean: Boolean
get() = sharedPrefs.getBoolean(KEY_MY_BOOLEAN, false)
set(value) = sharedPrefs.edit { putBoolean(KEY_MY_BOOLEAN, value) }
var myStringArray: Array<String>
get() = sharedPrefs.getStringSet(KEY_MY_ARRAY, emptySet())?.toTypedArray()
?: emptyArray()
set(value) = sharedPrefs.edit { putStringSet(KEY_MY_ARRAY, value.toSet()) }
Here, sharedPrefs.edit{...} is provided by the android core ktx library and should be implemented by adding dependency implementation "androidx.core:core-ktx:1.0.2" in appliation level build.gradle.
You can get the instance of SharedPreferences by using code:
val prefs = Prefs(context)
Furthermore, you can create the Singleton object of Prefs and use from anywhere within the app.
val prefs: Prefs by lazy {
Prefs(App.instance)
}
where, App extends Application and should be included in AndroidManifest.xml
App.kt
class App:Application() {
companion object {
lateinit var instance: App
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest .....
<application
android:name=".App"
....
Example Usage:
// get stored value
val myString = prefs.myString
// store value
prefs.myString = "My String Value"
// get stored array
val myStringArray = prefs.myStringArray
// store array
prefs.myStringArray = arrayOf("String 1","String 2","String 3")
This is my way
for write
SharedPreferences settings = context.getSharedPreferences("prefs", 0);
SharedPreferences.Editor editore = settings.edit();
editore.putString("key", "some value");
editore.apply();
to read
SharedPreferences settings = getSharedPreferences("prefs", 0);
Strings value = settings.getString("key", "");
Let's assume in a project, with multiple developers working on it, they are defining SharedPreference within an Activity like this:
SharedPreferences sharedPref = context.getSharedPreferences("prefName", 0);
At one point or another two developers can define the SharedPreference with the same name or insert equivalent Key - Value pairs, which will lead to problems in using the keys.
The solution relies on two options, whether to use;
SharedPreferences Singleton that uses String keys.
SharedPreferences Singleton that uses Enum keys.
Personally and According to this Sharepreference Documentation, I prefer to use Enum keys as it enforces stricter control when there are multiple programmers working on a project. A programmer has no choice but to declare a new key in the appropriate enum class and so all the keys are in the same place.
And to avoid boilerplate code writing create the SharedPreference singleton.
This SharedPreferences singleton Class help to centralize and simplify reading and writing of SharedPreferences in your Android app.
The source code for the two provided solutions can be found in GitHub

Categories