SharedPreference Changes not reflected in my wallp

2019-04-09 15:12发布

问题:

I am making a live wallpaper where i need to change the speed of a vehicle in setting scene and it needs to get reflected back to the wallpaper service when i press the return button. In my preference activity, i save the list preference changes in shared preferences like this :-

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);


    ListPreference listPreference = (ListPreference) findPreference("listPref");
    currValue = listPreference.getValue();
    Log.e("LiveWallpaperSettings", "currvalue " + currValue);

    listPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference arg0, Object arg1) {

            SharedPreferences customSharedPreference = getSharedPreferences("Speed", LiveWallpaperSettings.MODE_PRIVATE);
            SharedPreferences.Editor editor = customSharedPreference.edit();
            editor.putString("Speed",currValue);
            editor.commit();

            return true;
        }

    });

My wallpaper service is made using andengine livewallpaper extension. If i want to reflect the changes in my list preference in the service, how do i do that. This is what i did but it doesn't seem to be working.

My prefs.xml

 <PreferenceCategory
            android:title="Settings">

            <ListPreference
                    android:title="Speed"
                    android:summary="Change the Speed"
                    android:key="listPref"
                    android:defaultValue="15"
                    android:entries="@array/listArray"
                    android:entryValues="@array/listValues" 
             />
</PreferenceCategory>

My array.xml

<resources>
<string-array name = "listArray">
    <item>Slow</item>
    <item>Medium</item>
    <item>Fast</item>
</string-array>
<string-array name = "listValues">
    <item>5</item>
    <item>15</item>
    <item>30</item>
</string-array>

In my service i implement SharedPreferences.OnSharedPreferenceChangeListener and implement the following method

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {

    sharedPreferences = getSharedPreferences("Speed", MODE_PRIVATE);
    strSpeedValue = sharedPreferences.getString("Speed", "5");

    fltSpeedValue = Integer.parseInt(strSpeedValue);
    final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 10);
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(fltSpeedValue, new Sprite(0,mCamera.getHeight() - this.mParallaxLayer.getHeight(),this.mParallaxLayer, getVertexBufferObjectManager())));
    autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0f, new Sprite(CAMERA_WIDTH/2 - 30, CAMERA_HEIGHT/2,this.mAutoLayer, getVertexBufferObjectManager())));
    mMainScene.setBackground(autoParallaxBackground);
}

@Override
protected void onResume() {
    super.onResume();
    // Set up a listener whenever a key changes

    PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {
    super.onPause();
    // Unregister the listener whenever a key changes
    PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
}

But the value that i am changing in my listpreference does not get changed in my service. Am i doing something wrong?

回答1:

Solved!

I was setting values incorrectly in my PreferenceActivity and i didn't implemement OnSharedPreferenceChangeListener properly.

Solution :-

 ListPreference listPreference;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);        
    listPreference = (ListPreference) findPreference("listPref");

}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    SharedPreferences customSharedPreference = getSharedPreferences(key, LiveWallpaperSettings.MODE_PRIVATE);
    SharedPreferences.Editor editor = customSharedPreference.edit();
    editor.putString("Speed",listPreference.getValue());
    editor.commit();
    Log.e("LiveWallpaperSettings", "Speed Value after setting " + customSharedPreference.getString("Speed", ""));
}



@Override
protected void onResume() {
    super.onResume();
    // Set up a listener whenever a key changes
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {
    super.onPause();
    // Unregister the listener whenever a key changes
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}


回答2:

A couple of things: Your XML says that your key is "listPref", so when you're reading in the value from the prefs, you want to check for this key (Speed is just the display title)

I'm not convinced the preference changed listener is useful... The way that I would suggest, is that in your onResume() you would read the new value out of the preferences and use this value. This should work fine as everytime you return to your app after changing preferences, onResume() should be called.