This is a mirror of official site: http://jasper-net.blogspot.com/

Using Android Preferences in a background service

| Thursday, August 18, 2011
n this post I will explain how I have used the Android Preferences in a background service. The preference is used as an interval for updating a listview with some simple text items.

Preferences

To create the preferences screen I added an Options Menu. This options menu is part of the HomeActivity. The HomeActivity simply shows a listview. When clicking on the options menu button it will start the SettingsActivity. This activity extends the PreferenceActivity.

The only thing that is really necessary in the SettingsActivity is to add the preferences that are specified in the corresponding xml. In my implementation I have added a method that sets the selected value below the title. In the screenshot on the left you can see that the summary shows 15.

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {
    public static final String UPDATE_PREF = "updatePref";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);
        setUpdatePreferences();
    }

    private void setUpdatePreferences() {
        ListPreference updatePref = (ListPreference) findPreference(UPDATE_PREF);
        updatePref.setSummary(updatePref.getValue());


Read more: J Team
QR: https://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://blog.jteam.nl/2011/08/12/using-android-preferences-in-a-background-service/

Posted via email from Jasper-net

0 comments: