共享preference变化不会反映在我的壁纸服务 [英] SharedPreference Changes not reflected in my wallpaper service

查看:131
本文介绍了共享preference变化不会反映在我的壁纸服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个动态壁纸,我需要改变车辆的速度设置场景,它需要得到反射回壁纸服务时,我preSS返回按钮。在我的preference活动,我保存的共享preferences列表preference的变化是这样的: -

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;
        }

    });

我的壁纸服务使用andengine livewallpaper分机上。如果我想反映在服务我的列表preference的变化,我如何做到这一点。这是我做的,但它似乎并不奏效。

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.

我的prefs.xml

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>

我array.xml

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>

在我的服务,我实现共享preferences.OnShared preferenceChangeListener并实现下面的方法

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);
}

不过,我在我的名单preference我改变的值不会在我的服务得到改变。难道我做错了什么?

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

推荐答案

解决了!

我在我的preferenceActivity设置不正确的价值观和我没有正确implemement OnShared preferenceChangeListener。

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

解决方案: -

 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);
}

这篇关于共享preference变化不会反映在我的壁纸服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆