fix: 修复定时壁纸更换不工作的问题
问题:定时更换被"返回桌面更换"开关绑架 - 当用户关闭"返回桌面更换"时,定时器不再更换壁纸 修复方案: - 添加 EXTRA_SCHEDULED 参数区分触发来源 - 定时器触发:scheduled=true,必须更换壁纸 - 返回桌面触发:scheduled=false,受配置开关控制 - 用户主动触发:force_change=true,强制更换 修改文件: - WallpaperChangeReceiver - 添加 scheduled 参数处理 - AlarmScheduler - 定时触发时设置 scheduled=true - AutoWallpaperService - 根据触发来源决定是否更换 - HomeFragment - 简化"立即更换"逻辑 - CurrentWallpaperFragment - 修复更换按钮逻辑 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
791f297123
commit
49f1b78faa
5 files changed
+66
-111
No files matched your search
@@ -15,6 +15,7 @@ public class WallpaperChangeReceiver extends BroadcastReceiver {
|
||||
// 内部广播:用于通知壁纸服务更换壁纸(避免循环)
|
||||
private static final String ACTION_REFRESH_WALLPAPER_SERVICE = "cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER_SERVICE";
|
||||
public static final String EXTRA_FORCE_CHANGE = "force_change";
|
||||
public static final String EXTRA_SCHEDULED = "scheduled"; // 是否为定时器触发
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
@@ -23,10 +24,11 @@ public class WallpaperChangeReceiver extends BroadcastReceiver {
|
||||
|
||||
// 提取 force_change 标志,默认为 true(用户主动触发的更换)
|
||||
boolean forceChange = intent.getBooleanExtra(EXTRA_FORCE_CHANGE, true);
|
||||
Log.i(TAG, "Force change: " + forceChange);
|
||||
boolean scheduled = intent.getBooleanExtra(EXTRA_SCHEDULED, false);
|
||||
Log.i(TAG, "Force change: " + forceChange + ", Scheduled: " + scheduled);
|
||||
|
||||
// 如果是定时触发的更换(非用户主动),检查省电模式
|
||||
if (!forceChange) {
|
||||
if (!forceChange && scheduled) {
|
||||
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
|
||||
// 检测是否处于 Doze 模式或省电模式
|
||||
@@ -54,16 +56,19 @@ public class WallpaperChangeReceiver extends BroadcastReceiver {
|
||||
// 转发广播给 AutoWallpaperService,使用不同的 ACTION 避免循环
|
||||
Intent serviceIntent = new Intent(ACTION_REFRESH_WALLPAPER_SERVICE);
|
||||
serviceIntent.putExtra(EXTRA_FORCE_CHANGE, forceChange);
|
||||
serviceIntent.putExtra(EXTRA_SCHEDULED, scheduled); // 传递 scheduled 参数
|
||||
context.sendBroadcast(serviceIntent);
|
||||
|
||||
// 如果服务已启用,调度下一次更换
|
||||
cn.hugh.bz.gk2.wallpaper.WallpaperPreferences prefs =
|
||||
new cn.hugh.bz.gk2.wallpaper.WallpaperPreferences(context);
|
||||
cn.hugh.bz.gk2.model.AppConfig config = prefs.loadConfig();
|
||||
// 如果服务已启用,调度下一次更换(仅对定时触发)
|
||||
if (!forceChange && scheduled) {
|
||||
cn.hugh.bz.gk2.wallpaper.WallpaperPreferences prefs =
|
||||
new cn.hugh.bz.gk2.wallpaper.WallpaperPreferences(context);
|
||||
cn.hugh.bz.gk2.model.AppConfig config = prefs.loadConfig();
|
||||
|
||||
if (config.isEnabled()) {
|
||||
AlarmScheduler scheduler = new AlarmScheduler(context);
|
||||
scheduler.scheduleWallpaperChange(config.getIntervalMinutes());
|
||||
if (config.isEnabled()) {
|
||||
AlarmScheduler scheduler = new AlarmScheduler(context);
|
||||
scheduler.scheduleWallpaperChange(config.getIntervalMinutes());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,41 +123,18 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
return;
|
||||
}
|
||||
|
||||
// 直接获取下一张壁纸,与"下一张"按钮相同的逻辑
|
||||
cn.hugh.bz.gk2.model.WallpaperImage nextImage = null;
|
||||
String lastUri = prefs.getLastUri();
|
||||
// 发送广播通知壁纸服务立即更换壁纸
|
||||
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER");
|
||||
intent.putExtra("force_change", true); // 用户主动触发
|
||||
intent.putExtra("scheduled", false); // 非定时触发
|
||||
requireActivity().sendBroadcast(intent);
|
||||
|
||||
if (config.getPlayMode() == cn.hugh.bz.gk2.model.PlayMode.SEQUENTIAL) {
|
||||
// 顺序模式:获取下一张
|
||||
int index = config.getCurrentIndex();
|
||||
nextImage = scanner.getImageAtIndex(config.getSelectedFolders(), index);
|
||||
if (nextImage != null) {
|
||||
config.setCurrentIndex(index + 1);
|
||||
}
|
||||
} else {
|
||||
// 随机模式:随机选择一张
|
||||
nextImage = scanner.getRandomImage(config.getSelectedFolders(), lastUri);
|
||||
}
|
||||
Toast.makeText(requireContext(), "壁纸已更换", Toast.LENGTH_SHORT).show();
|
||||
|
||||
if (nextImage != null) {
|
||||
// 保存新壁纸配置
|
||||
prefs.saveConfig(config);
|
||||
prefs.saveLastUri(nextImage.getUri());
|
||||
|
||||
// 发送广播通知壁纸服务更新显示
|
||||
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER");
|
||||
intent.putExtra("force_change", false); // 只通知更新,不重新选择壁纸
|
||||
requireActivity().sendBroadcast(intent);
|
||||
|
||||
Toast.makeText(requireContext(), "壁纸已更换", Toast.LENGTH_SHORT).show();
|
||||
|
||||
// 延迟刷新预览
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
loadCurrentWallpaper();
|
||||
}, 500);
|
||||
} else {
|
||||
Toast.makeText(requireContext(), "没有更多壁纸了", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
// 延迟刷新预览
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
loadCurrentWallpaper();
|
||||
}, 500);
|
||||
}
|
||||
|
||||
private void showNextWallpaper() {
|
||||
@@ -168,40 +145,18 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
return;
|
||||
}
|
||||
|
||||
cn.hugh.bz.gk2.model.WallpaperImage nextImage = null;
|
||||
String lastUri = prefs.getLastUri();
|
||||
// 发送广播通知壁纸服务切换到下一张
|
||||
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER");
|
||||
intent.putExtra("force_change", true); // 用户主动触发
|
||||
intent.putExtra("scheduled", false); // 非定时触发
|
||||
requireActivity().sendBroadcast(intent);
|
||||
|
||||
if (config.getPlayMode() == cn.hugh.bz.gk2.model.PlayMode.SEQUENTIAL) {
|
||||
// 顺序模式:获取下一张
|
||||
int index = config.getCurrentIndex();
|
||||
nextImage = scanner.getImageAtIndex(config.getSelectedFolders(), index);
|
||||
if (nextImage != null) {
|
||||
config.setCurrentIndex(index + 1);
|
||||
}
|
||||
} else {
|
||||
// 随机模式:随机选择一张
|
||||
nextImage = scanner.getRandomImage(config.getSelectedFolders(), lastUri);
|
||||
}
|
||||
Toast.makeText(requireContext(), "已切换到下一张", Toast.LENGTH_SHORT).show();
|
||||
|
||||
if (nextImage != null) {
|
||||
// 保存新壁纸配置
|
||||
prefs.saveConfig(config);
|
||||
prefs.saveLastUri(nextImage.getUri());
|
||||
|
||||
// 发送广播通知壁纸服务更新显示
|
||||
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER");
|
||||
intent.putExtra("force_change", false); // 只通知更新,不重新选择壁纸
|
||||
requireActivity().sendBroadcast(intent);
|
||||
|
||||
Toast.makeText(requireContext(), "已切换到下一张", Toast.LENGTH_SHORT).show();
|
||||
|
||||
// 延迟刷新预览
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
loadCurrentWallpaper();
|
||||
}, 500);
|
||||
} else {
|
||||
Toast.makeText(requireContext(), "没有更多壁纸了", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
// 延迟刷新预览
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
loadCurrentWallpaper();
|
||||
}, 500);
|
||||
}
|
||||
|
||||
private void openInGallery() {
|
||||
|
||||
@@ -227,36 +227,13 @@ public class HomeFragment extends Fragment {
|
||||
return;
|
||||
}
|
||||
|
||||
// 直接获取下一张壁纸,与"下一张"按钮相同的逻辑
|
||||
cn.hugh.bz.gk2.model.WallpaperImage nextImage = null;
|
||||
String lastUri = prefs.getLastUri();
|
||||
// 发送广播通知壁纸服务立即更换壁纸
|
||||
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER");
|
||||
intent.putExtra("force_change", true); // 用户主动触发
|
||||
intent.putExtra("scheduled", false); // 非定时触发
|
||||
requireActivity().sendBroadcast(intent);
|
||||
|
||||
if (config.getPlayMode() == cn.hugh.bz.gk2.model.PlayMode.SEQUENTIAL) {
|
||||
// 顺序模式:获取下一张
|
||||
int index = config.getCurrentIndex();
|
||||
nextImage = scanner.getImageAtIndex(config.getSelectedFolders(), index);
|
||||
if (nextImage != null) {
|
||||
config.setCurrentIndex(index + 1);
|
||||
}
|
||||
} else {
|
||||
// 随机模式:随机选择一张
|
||||
nextImage = scanner.getRandomImage(config.getSelectedFolders(), lastUri);
|
||||
}
|
||||
|
||||
if (nextImage != null) {
|
||||
// 保存新壁纸配置
|
||||
prefs.saveConfig(config);
|
||||
prefs.saveLastUri(nextImage.getUri());
|
||||
|
||||
// 发送广播通知壁纸服务更新显示
|
||||
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER");
|
||||
intent.putExtra("force_change", false); // 只通知更新,不重新选择壁纸
|
||||
requireActivity().sendBroadcast(intent);
|
||||
|
||||
Toast.makeText(requireContext(), "壁纸已更换", Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(requireContext(), "没有更多壁纸了", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
Toast.makeText(requireContext(), "壁纸已更换", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void openFolderPicker() {
|
||||
|
||||
@@ -32,8 +32,9 @@ public class AlarmScheduler {
|
||||
// Create pending intent
|
||||
Intent intent = new Intent(context, WallpaperChangeReceiver.class);
|
||||
intent.setAction(WallpaperChangeReceiver.ACTION_CHANGE_WALLPAPER);
|
||||
// 定时器触发的更换不是强制更换,会根据配置决定是否更换
|
||||
// 定时器触发的更换:非强制,但标记为 scheduled
|
||||
intent.putExtra(WallpaperChangeReceiver.EXTRA_FORCE_CHANGE, false);
|
||||
intent.putExtra(WallpaperChangeReceiver.EXTRA_SCHEDULED, true);
|
||||
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
|
||||
@@ -48,7 +48,8 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
if ("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER_SERVICE".equals(intent.getAction())) {
|
||||
if (engine != null) {
|
||||
boolean forceChange = intent.getBooleanExtra("force_change", false);
|
||||
engine.refreshWallpaper(forceChange);
|
||||
boolean scheduled = intent.getBooleanExtra("scheduled", false);
|
||||
engine.refreshWallpaper(forceChange, scheduled);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,6 +66,7 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
private int height;
|
||||
private boolean visible = false;
|
||||
private boolean forceChangeNext = false;
|
||||
private boolean scheduledNext = false;
|
||||
|
||||
public WallpaperEngine() {
|
||||
this.imageLoader = new ImageLoader(AutoWallpaperService.this);
|
||||
@@ -111,10 +113,13 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
this.visible = false;
|
||||
}
|
||||
|
||||
public void refreshWallpaper(boolean forceChange) {
|
||||
public void refreshWallpaper(boolean forceChange, boolean scheduled) {
|
||||
if (forceChange) {
|
||||
forceChangeNext = true;
|
||||
}
|
||||
if (scheduled) {
|
||||
scheduledNext = true;
|
||||
}
|
||||
if (visible) {
|
||||
handler.post(this::drawWallpaper);
|
||||
}
|
||||
@@ -142,11 +147,22 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
|
||||
WallpaperImage image;
|
||||
|
||||
// 核心逻辑:根据配置决定是否更换壁纸
|
||||
boolean shouldChange = forceChangeNext || config.isChangeOnReturnToHome();
|
||||
// 核心逻辑:根据触发来源决定是否更换壁纸
|
||||
boolean shouldChange = false;
|
||||
|
||||
if (forceChangeNext) {
|
||||
// 用户主动触发(如点击"立即更换"):强制更换
|
||||
shouldChange = true;
|
||||
} else if (scheduledNext) {
|
||||
// 定时器触发:必须更换(这是主要的自动更换机制)
|
||||
shouldChange = true;
|
||||
} else if (config.isChangeOnReturnToHome()) {
|
||||
// 返回桌面触发:根据配置决定
|
||||
shouldChange = true;
|
||||
}
|
||||
|
||||
if (shouldChange) {
|
||||
// 强制更换或开启返回桌面更换:获取下一张壁纸
|
||||
// 获取下一张壁纸
|
||||
image = getNextWallpaper(config);
|
||||
|
||||
if (image != null) {
|
||||
@@ -157,7 +173,7 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
drawPlaceholder(canvas);
|
||||
}
|
||||
} else {
|
||||
// 关闭状态:使用当前壁纸(不更换)
|
||||
// 不更换:使用当前壁纸
|
||||
String lastUri = prefs.getLastUri();
|
||||
if (lastUri != null) {
|
||||
// 复用当前壁纸
|
||||
@@ -175,8 +191,9 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
}
|
||||
}
|
||||
|
||||
// 重置强制更换标志
|
||||
// 重置标志
|
||||
forceChangeNext = false;
|
||||
scheduledNext = false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error drawing wallpaper", e);
|
||||
|
||||
Reference in new issue
Block a user