Search code examples
androidbroadcastreceiverandroid-preferencespreferenceactivity

A Bunch of questions regarding Android PreferenceActivity


I'm using PreferenceActivity to set some preferences about another BroadcastReciever that i have.

While making the skeleton of the preferences GUI is really simple with XML file, i cant find my hands and legs around how to actually DO something with those preferences. especially:

  1. How do i register my BroadcastReciever to listen to changes in my preferences? Its look like that the context of the broadcast reciever is not the same as the activity so i'm not registering to the right SharedPreferences because i dont intercept those changes.

  2. How do i act in my PreferenceActivity upon changes in the preferences? i guess i dont need to register PreferenceActivity as listener to the preferences, so there must be more simple way.

  3. How do i change the UI of the PreferenceActivity appropriate to the changes in the preferences? for example, set the "Summary" attribute of a preference to the value that the user chose?

  4. How do i enable or disable some preferences that depends on another preference (like CheckBoxPreference)? so the user could edit those preferences only if he enabled the feature first.

I looked al over the documentation but there is no example of using PreferenceActivity beyond the point of just adding preferences from XML.

I feel like i'm missing something huge here, because it looks so simple, and yet i can't figure it out...

Please try to answer on any of my question.

BTW: I'm developing for Android 1.6 so all the new PreferenceFragment can't be used.


Solution

  • How do i register my BroadcastReciever to listen to changes in my preferences?

    If the BroadcastReceiver is registered in the manifest, you don't listen to changes in your preferences, because the BroadcastReceiver will only be around for milliseconds. Just read the latest values in during onReceive().

    If the BroadcastReceiver is registered by some other component calling registerReceiver(), that component can register an OnSharedPreferenceChangeListener with the SharedPreferences retrieved via PreferenceManager and getDefaultSharedPreferences().

    Its look like that the context of the broadcast reciever is not the same as the activity so i'm not registering to the right SharedPreferences because i dont intercept those changes.

    That sentence makes no sense.

    How do i act in my PreferenceActivity upon changes in the preferences?

    Register an OnSharedPreferenceChangeListener with the SharedPreferences retrieved via PreferenceManager and getDefaultSharedPreferences().

    How do i change the UI of the PreferenceActivity appropriate to the changes in the preferences?

    Generally, you don't. You are welcome to register an OnSharedPreferenceChangeListener with the SharedPreferences retrieved via PreferenceManager and getDefaultSharedPreferences(). From there, you can get the Preference from your PreferenceActivity via findPreference() and adjust to suit.

    How do i enable or disable some preferences that depends on another preference (like CheckBoxPreference)? so the user could edit those preferences only if he enabled the feature first.

    Add android:dependency to your preference XML as needed.