设置更新后如何重新启动此壁纸引擎? [英] How do I restart this wallpaper engine after settings have been updated?

查看:194
本文介绍了设置更新后如何重新启动此壁纸引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个动态壁纸,并以本教程为起点:

I'm creating a live wallpaper and I'm using this tutorial as a starting point:

http://code.tutsplus.com/tutorials/create-a-live-wallpaper-on-android-using-an-animated-gif--cms-23088

我正在尝试添加一个设置菜单,让用户可以选择四种背景之一.我使用 SharedPreferences 完成了所有工作.唯一的问题是在设置菜单中更改设置后壁纸不会更新.如果您重新启动应用程序,背景将更新为上次选择的背景,但只有在壁纸重新启动后才有效(即选择不同的壁纸,然后重新选择该壁纸).

I'm trying to add a settings menu to let the user choose one of four backgrounds. I have everything working using SharedPreferences. The only problem is that the wallpaper does not update after the setting is changed in the settings menu. If you restart the app, the background will be updated with the last background selected, but it only works after the wallpaper is restarted (i.e., selecting a different wallpaper, and then reselecting this wallpaper).

我已将问题缩小到这样一个事实,即 SharedPreference 中的值仅在 onCreateEngine() 方法中更新.一旦壁纸服务运行,onCreateEngine() 方法不会被调用,所以即使 SharedPreference 的值已经改变,它也不会更新在壁纸服务中.

I've narrowed the problem down to the fact that the value from the SharedPreference is only updated in the onCreateEngine() method. Once the wallpaper service is running, the onCreateEngine() method doesn't get called, so even though the value of the SharedPreference has been changed, it doesn't get updated in the wallpaper service.

我的问题是如何重新启动壁纸,以便在更改设置后调用 onCreateEngine() 方法?同样,SharedPreferences 正在工作,因为重启后一切正常.我知道我需要使用 onsharedPreferenceChanged 方法,但我不确定应该在哪里发生,或者该方法中应该包含什么代码来重新启动壁纸引擎.

My question is how do I restart the wallpaper so that the onCreateEngine() method gets called after a setting gets changed? Again, the SharedPreferences are working, since everything works after restart. I know I need to use the onsharedPreferenceChanged method, but I'm not sure where that should occur, or what code should be included in that method to restart the wallpaper engine.

这是示例代码.除了评论指出的地方外,我的也是一样的:

Here's the sample code. Mine is the same except where indicated by the comments:

    import android.graphics.Canvas;
    import android.graphics.Movie;
    import android.os.Handler;
    import android.service.wallpaper.WallpaperService;
    import android.util.Log;
    import android.view.SurfaceHolder;

    import java.io.IOException;

    public class GIFWallpaperService extends WallpaperService implements onsharedpreferencechangelistener {

        // Variable I added to change background
        String mBackgroundImage = null;

        // Method I added to update background image
        public void updatedBackgroundImage(){
             // code that sets mBackgroundImage based upon value of shared preference file. 
        }

        @Override
        public WallpaperService.Engine onCreateEngine() {

            // I call this method to change the value of mBackgroundImage
            updateBackgroundImage(); 

            try {
                Movie movie = Movie.decodeStream(
                        getResources().getAssets().open("mBackgroundImage"));

                return new GIFWallpaperEngine(movie);
            }catch(IOException e){
                Log.d("GIF", "Could not load asset");
                return null;
            }
        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
    // What do I do?  How do I make the wallpaper engine restart when settings are changed?  
        }

        private class GIFWallpaperEngine extends WallpaperService.Engine {

            private final int frameDuration = 20;

            private SurfaceHolder holder;
            private Movie movie;
            private boolean visible;
            private Handler handler;

            public GIFWallpaperEngine(Movie movie) {
                this.movie = movie;
                handler = new Handler();
            }

            @Override
            public void onCreate(SurfaceHolder surfaceHolder) {
                super.onCreate(surfaceHolder);
                this.holder = surfaceHolder;
            }

            private Runnable drawGIF = new Runnable() {
                public void run() {
                    draw();
                }
            };


            private void draw() {
                if (visible) {
                    Canvas canvas = holder.lockCanvas();
                    canvas.save();
                        // Adjust size and position so that
                        // the image looks good on your screen
                        canvas.scale(3f, 3f);
                        movie.draw(canvas, -100, 0);
                    canvas.restore();
                    holder.unlockCanvasAndPost(canvas);
                    movie.setTime((int) (System.currentTimeMillis() % movie.duration()));

                    handler.removeCallbacks(drawGIF);
                    handler.postDelayed(drawGIF, frameDuration);
                }
            }

            @Override
            public void onVisibilityChanged(boolean visible) {
                this.visible = visible;
                if (visible) {
                    handler.post(drawGIF);
                } else {
                    handler.removeCallbacks(drawGIF);
                }
            }

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

推荐答案

我可能迟到了,但希望有人会觉得这有用.

I'm propably late, but hope someone will find this useful.

您需要先指定并注册您的 SharedPrefs.将此添加到您的 onCreate() 方法中.

You need to specify and register you SharedPrefs first. Add this to your onCreate() method.

SharedPreferences preferences = getSharedPreferences("PREFERENCES NAME", Context.MODE_PRIVATE);
        preferences.registerOnSharedPreferenceChangeListener(this);

这篇关于设置更新后如何重新启动此壁纸引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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