你有两个选择。要么检查首选项值onVisibilityChanged()
并相应地更新壁纸。或者使用 anOnSharedPreferenceChangeListener
让您Engine
知道用户何时更改了偏好。
后者的例子。
让你Engine
实现OnSharedPreferenceChangeListener
接口。
private class MyEngine extends Engine implements OnSharedPreferenceChangeListener {
// A reference to our shared prefs;
private SharedPreferences mPreferences;
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
// Register an OnSharedPreferenceChangeListener to our shared prefs
mPreferences.registerOnSharedPreferenceChangeListener(this);
// Your existing code ...
}
@Override
public void onDestroy() {
super.onDestroy();
// Don't forget to unregister the listener
mPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Whenever the user changes a setting, this method will be called.
// So do what needs to be done here, like redrawing the wallpaper
redrawWallpaper();
}
}