androidsettingslive-wallpaper

Retrieving data from settings page in live wallpaper


Consider following code be the settings page of a live wallpaper in Android:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/livewallpaper_settings"
    android:key="livewallpaper_settings">

    <ListPreference
        android:key="livewallpaper_testpattern"
        android:title="@string/livewallpaper_settings_title"
        android:summary="@string/livewallpaper_settings_summary"
        android:entries="@array/livewallpaper_testpattern_names"
        android:entryValues="@array/livewallpaper_testpattern_prefix"/>
    <CheckBoxPreference android:key="livewallpaper_movement"
        android:summary="@string/livewallpaper_movement_summary"
        android:title="@string/livewallpaper_movement_title"
        android:summaryOn="Moving test pattern"
        android:summaryOff="Still test pattern"/>
</PreferenceScreen>

It shows a setting page and everything about showing the settings is ok. How can I save this settings and use them while creating the live wallpaper?

Also, is it true to read the settings in onCreate method or not?


Solution

  • I found the answer :

    package ca.jvsh.livewallpaper;
    
    import ca.jvsh.livewallpaper.R;
    
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceActivity;
    
    public class LiveWallpaperSettings extends PreferenceActivity
        implements SharedPreferences.OnSharedPreferenceChangeListener
    {
        @Override
        protected void onCreate(Bundle icicle)
        {
            super.onCreate(icicle);
            getPreferenceManager().setSharedPreferencesName(LiveWallpaper.SHARED_PREFS_NAME);
            addPreferencesFromResource(R.xml.livewallpaper_settings);
            getPreferenceManager().getSharedPreferences()
                    .registerOnSharedPreferenceChangeListener(this);
        }
    
        @Override
        protected void onResume()
        {
            super.onResume();
        }
    
        @Override
        protected void onDestroy()
        {
            getPreferenceManager().getSharedPreferences()
                    .unregisterOnSharedPreferenceChangeListener(this);
            super.onDestroy();
        }
    
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                String key)
        {
        }
    }
    

    this was the java class for the setting page. and this is for reading the settings :

        TestPatternEngine()
        {
            ...
    
            mPreferences = LiveWallpaper.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
            mPreferences.registerOnSharedPreferenceChangeListener(this);
            onSharedPreferenceChanged(mPreferences, null);
        }
    
        public void onSharedPreferenceChanged(SharedPreferences prefs,
                String key)
        {
            mShape = prefs.getString("livewallpaper_testpattern", "smpte");
            mMotion = prefs.getBoolean("livewallpaper_movement", true);
            readColors();
        }