1

I use CheckBoxPreference in my PreferenceActivity to set a value. Later on, I want to check that value from Receiver and/or Service. findPreference() method is not available from that context. I know, that this preference value is stored in SharedPreferences anyway, but what is the key? How could I get the value of the checkbox?

jacek
  • 947
  • 1
  • 11
  • 20

4 Answers4

6

I know, that this preference value is stored in SharedPreferences anyway, but what is the key?

Whatever value you have for android:key in your preference XML.

How could I get the value of the checkbox?

Call PreferenceManager.getDefaultSharedPreferences() to get the SharedPreferences, then call getBoolean() with the key you used in android:key.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

In your preferences XML you'll have something like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:enabled="true"
    android:title="@string/s_pref" android:key="@string/pref"
    android:defaultValue="@string/d_pref"></CheckBoxPreference>
</PreferenceScreen>

Your strings.xml would have something like this:

<string name="pref">my.package.PREF</string>
<string name="s_pref">Prompt</string>
<string name="d_pref">true</string>

Your Activity's onCreate() would have something like this:

prefs = PreferenceManager.getDefaultSharedPreferences(this);
pref = prefs.getBoolean(getString(R.string.pref), true));

If you want to do something when someone changes the preferences, add an onActivityResult() to your activity and start the preferences activity with startActivityForResult(). When onActivityResult() is invoked with whatever result code you want to indicate a change in preferences, you can do another getDefaultSharedPreferences().

The shared preferences framework automatically persists the data... you don't have to actively deal with it yourself, though you can if you wish with an OnPreferenceChangeListener in the preferences activity

Earl
  • 791
  • 4
  • 9
0

try to write this in your service

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplication());

and here specify the key that you have used in xml

 if(preferences.getBoolean(your key ,true))
            {

hope this help .

Mohamed Hocine
  • 575
  • 6
  • 10
0

The only thing I would add to CommonsWare's answer is, since you mentioned a service, you can put whatever preferences the service needs to know about in its Intent extras. For example:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Intent intent = new Intent(this, MyService.class);
intent.putExtra("mypref", prefs.getString("mypref", ""));
startService(intent);
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
Earl
  • 791
  • 4
  • 9
  • In response to your comment the documentation, there is actually a pretty good discussion, with example code, here: http://developer.android.com/guide/topics/data/data-storage.html#pref – Earl Jul 31 '11 at 18:36
  • I haven't found anything about retrieving data from built-in `CheckBoxPreference` (other than getting the `CheckBoxPreference` itself). – jacek Jul 31 '11 at 18:58