gpt4 book ai didi

java - Android 壁纸 OnSharedPreferenceChangeListener

转载 作者:行者123 更新时间:2023-12-02 00:31:30 27 4
gpt4 key购买 nike

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

更新:我已经做了,如示例中所写,但结果没有改变..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);
}
}
}
}

首选项.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>

最佳答案

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

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

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

关于java - Android 壁纸 OnSharedPreferenceChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8960631/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com