-1

I'm using context.getSharedPreference instead of getDefaultSharedPreferences. Means :

SharedPreferences checkboxSetting = context.getSharedPreferences(
                                         "myPreferenceDB", Context.MODE_PRIVATE);
boolean flag = checkboxSetting.getBoolean("checkboxKey",true);

And preference.xml :

<CheckBoxPreference 
        android:key="checkBoxPrefff"
        android:title="@string/title"
        android:defaultValue="true"/>

Application setting has a checkboxpreference that I want to get the value of, checked or not checked.

Will this work?

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                                                      String key) {
    if (key.equalsIgnoreCase("checkBoxPrefff")) { 
         sharedPreferences.getBoolean(key,true);
    }
 }

Does sharedPreferences use default database ( ..._preference.xml ) or my defined database ( "myPreferenceDB" )? Does key is "checkboxKey" or is null?

Because when I want to get value like

SharedPreferences temp = context.getSharedPreferences(
                                         "myPreferenceDB", Context.MODE_PRIVATE);
boolean flag = temp.getBoolean("checkboxKey",true);

it's wrong and gets back defValue (true) . But when use like

SharedPreferences temp = PreferenceManager.getDefaultSharedPreferences(context);
boolean flag = temp.getBoolean("checkboxKey",true);

it works.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
  • "Does sharedPreferences use default database ( ..._preference.xml ) or my defined database ( "myPreferenceDB" )" - it depends on what _you poass to the function_. Also is your key `checkboxKey` or `checkBoxPrefff`. And also `equalsIgnoreCase` ? _equalsIgnoreCase_ ? – Mr_and_Mrs_D May 01 '13 at 17:38

2 Answers2

1

You can register a SharedPreferenceChangedListener on any shared pref that you are using. It need not be the default shared preference.

getApplicationContext().getSharedPreferences("myPreferenceDB", 0).registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                String key) {
            // TODO Auto-generated method stub

        }
    });
Abhishek Nandi
  • 4,265
  • 1
  • 30
  • 43
  • Thanks,but it's not my related answer! i want to know does sharedPreferences use default database or my defined database? – Dr.jacky Nov 10 '12 at 06:34
  • What's different between onPreferenceChange and onSharedPreferenceChanged? – Dr.jacky Nov 10 '12 at 11:24
  • sharedPref is an xml file which is stored under /data/data//files directory. Can you tell me what are you trying to achieve ? – Abhishek Nandi Nov 11 '12 at 13:20
  • I want to use my own sharedPref ( not default name ) and don't write any thing for insert values to it ( edit().commit() ) , and just use addPreferencesFromResource(R.xml.preferences); that handle all insert and update of values to database – Dr.jacky Nov 11 '12 at 13:23
1

Once again I would like to mention sharedPref is not a DB but rather an XML file. I hope you are looking for the following code.

public class SettingsActivity extends PreferenceActivity {

    /*
     * (non-Javadoc)
     * 
     * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName("myPref"); 
        addPreferencesFromResource(R.xml.pref);
    }
Abhishek Nandi
  • 4,265
  • 1
  • 30
  • 43
  • Thanks,but sharedPreferences are DB ,typical.by the way,getPreferenceManager is deprecated . what's instead of? – Dr.jacky Nov 12 '12 at 08:22
  • 2-If i use setSharedPreferencesName and change default name,no need to add values manually (ex : myPrefIns.edit().putString("myKey",valueeee).commit()) ? – Dr.jacky Nov 12 '12 at 08:54
  • 1
    getPreferenceManager() is deprecated from API11. You simply want to have a settings like screen and wish to store the values in the shared pref then you don't need to manually save it. Also see- http://developer.android.com/reference/android/preference/PreferenceActivity.html, I you wish to store values from anywhere else like any other activity/service, etc you have to do it manually. – Abhishek Nandi Nov 12 '12 at 09:09
  • Yes,i know it's deprecated from 11,and i set minSdkVErsion to 5 .So what's happen? – Dr.jacky Nov 12 '12 at 09:13
  • Nothing. Did you try implement this ? – Abhishek Nandi Nov 12 '12 at 09:18
  • Yes,i use it.i just wanted to know what's happen in 11+.Nothing?! :-o so why document says it's deprecated?! – Dr.jacky Nov 12 '12 at 10:54
  • The reason for deprecation is specified as "This function is not relevant for a modern fragment-based PreferenceActivity. " Since your minSDKVersion is 5 you can go ahead and use this. You will not face any problem – Abhishek Nandi Nov 12 '12 at 11:36