FileDocCategorySizeDatePackage
AdvancedPreferences.javaAPI DocAndroid 1.5 API3348Wed May 06 22:41:08 BST 2009com.example.android.apis.app

AdvancedPreferences

public class AdvancedPreferences extends android.preference.PreferenceActivity implements android.content.SharedPreferences.OnSharedPreferenceChangeListener
Example that shows finding a preference from the hierarchy and a custom preference type.

Fields Summary
public static final String
KEY_MY_PREFERENCE
public static final String
KEY_ADVANCED_CHECKBOX_PREFERENCE
private android.preference.CheckBoxPreference
mCheckBoxPreference
private android.os.Handler
mHandler
private Runnable
mForceCheckBoxRunnable
This is a simple example of controlling a preference from code.
Constructors Summary
Methods Summary
protected voidonCreate(android.os.Bundle savedInstanceState)

    
    
        
        super.onCreate(savedInstanceState);

        // Load the XML preferences file
        addPreferencesFromResource(R.xml.advanced_preferences);
        
        // Get a reference to the checkbox preference
        mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
                KEY_ADVANCED_CHECKBOX_PREFERENCE);
    
protected voidonPause()

        super.onPause();

        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        
        mHandler.removeCallbacks(mForceCheckBoxRunnable);
    
protected voidonResume()

        super.onResume();

        // Start the force toggle
        mForceCheckBoxRunnable.run();
        
        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    
public voidonSharedPreferenceChanged(android.content.SharedPreferences sharedPreferences, java.lang.String key)

        // Let's do something when my counter preference value changes
        if (key.equals(KEY_MY_PREFERENCE)) {
            Toast.makeText(this, "Thanks! You increased my count to "
                    + sharedPreferences.getInt(key, 0), Toast.LENGTH_SHORT).show();
        }