Implementing SharedPreferences for Android to save to a database?

I am trying to use the SharedPreferences user interface SharedPreferences to store application settings in a database (instead of default XML).

Why? I would like to use the convenience of PreferenceActivity to create a user interface, but this application will be used on several devices, so the preferences that the user sets must be saved on all devices.

So, I have an implementation, but I canโ€™t figure out how to associate a particular SharedPreference with PreferenceActivity . I know that uses getSharedPreferences() , but how can I override this? Can I register my SharedPreferences some way? I have not seen anything in the API for this.

Or ... if I'm going to all this wrong, let me know. Thanks.

+3
source share
4 answers

I studied this question a while ago. For another reason.

I found that PreferenceActivity is closely related to PreferenceManager , and it uses PreferenceManager.getDefaultSharedPreferences() inside to get an instance of SharedPreference . And there is no way to replace a custom instance of PreferenceManager or SharedPreference inside PrefernceActivity .

I found that the preferential structure is inflexible and it is rather difficult to extend or change the default behavior, and sometimes itโ€™s even impossible. But I assume that the developers of this framework had โ€œease of useโ€ as their top priority, not extensibility and flexibility. It's clear.

+7
source

After some research and the emergence of this particular need, there is a โ€œsimpleโ€ solution.

You need to redefine both SharedPreferences and Editor to provide your own implementation, then each Preferences must also be expanded to redefine the various persistXXX () and getPersistedXXX () methods.

For example, CheckBoxPreference can override persistBoolean and getPersistedBoolean to do the trick.

Unfortunately, you cannot simply extend the PreferenceManager class to override getPreferenceManager () in each preference class.

0
source

It was easy for me to work: just set all the settings persistent="false" (either via XML or via preference.setPersistent(false) in the code). Then set OnPreferenceChangeListener to be notified when settings are changed, and let the handler store the values โ€‹โ€‹in some user database.

Here is my code:

 Preference preference = findPreference(getString(R.string.pref1)); Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object value) { preference.setSummary((String)value + " something"); // store value in custom database store((String)value); return true; } }; preference.setOnPreferenceChangeListener(changeListener); // update preference from custom database (once) changeListener.onPreferenceChange(preference, getValueFromDatabase()); 
0
source

I am working on settings that are stored on a Bluetooth device. My solution is to override the getSharedPreferences method of my Activity:

 @Override public SharedPreferences getSharedPreferences (String name, int mode) { return new MySharedPreferences(this); } 

MySharedPreference is my implementation of the android.content.SharedPreferences interface. You must also implement its inner classes. In the constructor of MySharedPreference, activity is passed as a parameter and stored in a member variable

0
source

All Articles