Android 壁纸 OnSharedPreferenceChangeListener

发布于 2024-12-28 05:47:04 字数 4757 浏览 0 评论 0原文

我为 android 制作壁纸,我希望用户可以选择选项。显示带有选项的菜单,但有问题。当我单击任何选项并返回壁纸屏幕时,它们不会更新新选项。为什么? 我的代码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>

I do wallpapers to andoid and I want that the user could choose options. Menu with options is showed but it have problem. When i click any options and go back to wallpapers screen, they dont update with new options. Why?
My code 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:
I have made, as was written in an example, but the result hasn't changed..
LiveWallpaperService code:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何时共饮酒 2025-01-04 05:47:04

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

请检查您是否完全按照以下方式实现了 OnSharedPreferenceChangeListener
http://developer.android .com/resources/samples/CubeLiveWallpaper/src/com/example/android/livecubes/cube2/CubeWallpaper2.html

如果您这样做了但仍然没有正在工作,请在此处发布您的整个 WallpaperService 和设置首选项活动代码。

Your wallpaper will never restart after coming back from settings. The only method invoked will be your Engine's onVisibilityChanged. If your correctly implements SharedPreferences.OnSharedPreferenceChangeListener on your Engine, then onSharedPreferenceChanged should be called too.

Please check if you implemented OnSharedPreferenceChangeListener exactly this way:
http://developer.android.com/resources/samples/CubeLiveWallpaper/src/com/example/android/livecubes/cube2/CubeWallpaper2.html

If you did and it is still not working, please post your entire WallpaperService and settings preference activity code here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文