简体   繁体   中英

android wallpapers OnSharedPreferenceChangeListener

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM