Question

I am creating a livewallpaper with andengine. my project have two classes

public class LiveWallpaperMain extends BaseLiveWallpaperService implements SharedPreferences.OnSharedPreferenceChangeListener
{

//Other methods

    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
            throws Exception {
        // TODO Auto-generated method stub
        this.mEngine.registerUpdateHandler(new FPSLogger());
        scene = new Scene();


        if(snowDensity == 5)
            snowDensityValue = 50;
        else if(snowDensity == 10)
            snowDensityValue = 200;
        else
            snowDensityValue = 500;
        particleSystem = new BatchedPseudoSpriteParticleSystem(
                new RectangleParticleEmitter(CAMERA_WIDTH / 2, 0, CAMERA_WIDTH, CAMERA_HEIGHT),
                2, 5, snowDensityValue, mSnowParticleRegion, vbom);
        particleSystem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE);
        particleSystem.addParticleInitializer(new VelocityParticleInitializer<Entity>(-3, 3, 20, 40));
        particleSystem.addParticleInitializer(new AccelerationParticleInitializer<Entity>(-3, 3, 3, 5));
        particleSystem.addParticleInitializer(new RotationParticleInitializer<Entity>(0.0f, 360.0f));
        particleSystem.addParticleInitializer(new org.andengine.entity.particle.modifier.ExpireParticleInitializer<Entity>(15f));
        particleSystem.addParticleInitializer(new ScaleParticleInitializer<Entity>(0.5f, 0.9f));
        particleSystem.addParticleInitializer(new RegisterXSwingEntityModifierInitializer<Entity>(15f, 0f, (float) Math.PI * 8, 3f, 25f, true));

        particleSystem.addParticleModifier(new AlphaParticleModifier<Entity>(10f, 15f, 1.0f, 0.0f));
    Log.e("LiveWallpaperSettings", "Density " + snowDensityValue);



    pOnCreateSceneCallback.onCreateSceneFinished(scene);
}

}

And this is the settings class

public class LiveWallpaperSettings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
{   
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
        SharedPreferences customSharedPreference = getSharedPreferences(key, MODE_PRIVATE);
        SharedPreferences.Editor editor = customSharedPreference.edit();
        editor.putString("Speed",listPreference.getValue());
        editor.commit();
        Log.e("LiveWallpaperSettings", "currvalue " + listPreference.getValue());
        currValue = listPreference.getValue();
    }
}

I want to change the snow density when it is changed in the settings. When i change settings, it get changed in the sharedpreferences but how can i refresh the particle system to the changes get reflected there instantly?

Was it helpful?

Solution

First of all, you don't need to implement OnSharedPreferenceChangeListener in your LiveWallpaperSettings class. If you have preference keys correctly defined in xml file, values will be stored automatically. Your class should look like this:

public class LiveWallpaperSettings extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        getPreferenceManager().setSharedPreferencesName("shared-preference-file-name");
        addPreferencesFromResource(R.xml.settings); // this is xml with PreferenceScreen element
    }
}

Implement OnSharedPreferenceChangeListener in your LiveWallpaperMain class. Implementation should look like this:

   public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if ("snow-density-key-name-from-xml".equals(key)) {
            // here call the method to update snow density
        }
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top