android壁纸OnSharedPreferenceChangeListener

时间:2012-01-22 11:12:49

标签: java android live-wallpaper

我做壁纸到andoid我希望用户可以选择选项。显示带有选项的菜单但有问题。当我点击任何选项并返回到壁纸屏幕时,他们不会使用新选项进行更新。为什么? 我的代码WallpaperService:

public MyWallpaperEngine(WallpaperService ws)
    {
        context = ws;
        prefs = LiveWallpaperService.this.getSharedPreferences(SHARED_PREFS_NAME, 0);

        OnSharedPreferenceChangeListener listener 
        = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
            {
                if(key != null){
                    if(key.equals("BACKREPEAT")){
                        if(BACKREPEAT)
                            BACKREPEAT = false;
                        else
                            BACKREPEAT = true;
                    }
                }
            }
        };
        prefs.registerOnSharedPreferenceChangeListener(listener);

        handler.post(drawRunner);
    }

UPD : 我已经做了,正如在一个例子中所写,但结果没有改变.. LiveWallpaperService代码:

package com.samples;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;

public class LiveWallpaperService extends WallpaperService
{
    public static final String SHARED_PREFS_NAME = "leoSettings01";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public Engine onCreateEngine() {
        return new MyWallpaperEngine();
    }

    private class MyWallpaperEngine extends Engine implements 
    SharedPreferences.OnSharedPreferenceChangeListener {

        private final Handler handler = new Handler();

        int draw_x = 0;
        int draw_y = 0;
        //...

        boolean BACKREPEAT = false;

        private final Runnable drawRunner = new Runnable() {
            @Override
            public void run()
            {
                draw();
            }
        };

        private boolean visible = true;
        private SharedPreferences prefs;

        MyWallpaperEngine()
        {
            prefs = LiveWallpaperService.this.getSharedPreferences(SHARED_PREFS_NAME, 0);

            prefs.registerOnSharedPreferenceChangeListener(this);
            onSharedPreferenceChanged(prefs, null);

            handler.post(drawRunner);
        }
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            if(key != null)
            {
                Log.v("key:", key); //no message!

                if(key.equals("BACKREPEAT")){
                    if(BACKREPEAT)
                        BACKREPEAT = false;
                    else
                        BACKREPEAT = true;
                }
            }
        }
        @Override
        public void onVisibilityChanged(boolean visible) {
            this.visible = visible;
            if (visible) {
            handler.post(drawRunner);
            } else {
            handler.removeCallbacks(drawRunner);
            }
        }
        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            this.visible = false;
            handler.removeCallbacks(drawRunner);
        }

        private void draw()
        {
            SurfaceHolder holder = getSurfaceHolder();
            Canvas canvas = null;

            try
            {
                canvas = holder.lockCanvas();
                //...draw draw draw
            }
            finally
            {
                if (canvas != null)
                        holder.unlockCanvasAndPost(canvas);
            }
            handler.removeCallbacks(drawRunner);
            if (visible)
            {
                handler.postDelayed(drawRunner, DELAY);
            }
        }   
    }
}

的prefs.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory android:title="General">
                <CheckBoxPreference
                        android:title="Animation repeat"
                        android:key="BACKREPEAT" 
                        android:defaultValue="false"
                />
        </PreferenceCategory>
</PreferenceScreen>

1 个答案:

答案 0 :(得分:3)

从设置中返回后,您的壁纸将永远不会重新启动。调用的唯一方法是您的引擎onVisibilityChanged。如果您在引擎上正确实施SharedPreferences.OnSharedPreferenceChangeListener,则也应调用onSharedPreferenceChanged

请检查您是否以这种方式实施OnSharedPreferenceChangeListenerhttp://developer.android.com/resources/samples/CubeLiveWallpaper/src/com/example/android/livecubes/cube2/CubeWallpaper2.html

如果您确实无法使用,请在此处发布您的整个 WallpaperService 和设置首选项活动代码。