diff --git a/app/src/main/java/cn/hugh/bz/gk2/ui/HomeFragment.java b/app/src/main/java/cn/hugh/bz/gk2/ui/HomeFragment.java index 336392e..b9635ef 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/ui/HomeFragment.java +++ b/app/src/main/java/cn/hugh/bz/gk2/ui/HomeFragment.java @@ -22,6 +22,7 @@ import java.util.List; import cn.hugh.bz.gk2.R; import cn.hugh.bz.gk2.model.Album; import cn.hugh.bz.gk2.model.AppConfig; +import cn.hugh.bz.gk2.utils.WallpaperServiceHelper; import cn.hugh.bz.gk2.wallpaper.AlbumPreferences; import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences; @@ -208,8 +209,13 @@ public class HomeFragment extends Fragment { } private void updateUI() { - // Update status - if (config.isEnabled()) { + // 检测壁纸服务是否真实在运行 + boolean isServiceActuallyRunning = WallpaperServiceHelper.isServiceRunning(requireContext()); + boolean isConfigEnabled = config.isEnabled(); + + // Update status - 优先使用实际运行状态 + if (isServiceActuallyRunning && isConfigEnabled) { + // 服务真正在运行 tvStatus.setText(R.string.status_enabled); btnToggle.setText(R.string.disable); btnChangeNow.setEnabled(true); @@ -218,7 +224,18 @@ public class HomeFragment extends Fragment { statusAnimation.setAnimation(R.raw.status_active); statusAnimation.playAnimation(); } + } else if (isConfigEnabled && !isServiceActuallyRunning) { + // 配置已启用但服务未运行(用户可能还没选择壁纸) + tvStatus.setText("等待设置壁纸"); + btnToggle.setText(R.string.disable); + btnChangeNow.setEnabled(false); + // 停止 Lottie 动画 + if (statusAnimation != null) { + statusAnimation.cancelAnimation(); + statusAnimation.setProgress(0f); + } } else { + // 服务已禁用 tvStatus.setText(R.string.status_disabled); btnToggle.setText(R.string.enable); btnChangeNow.setEnabled(false); diff --git a/app/src/main/java/cn/hugh/bz/gk2/utils/WallpaperServiceHelper.java b/app/src/main/java/cn/hugh/bz/gk2/utils/WallpaperServiceHelper.java new file mode 100644 index 0000000..46556da --- /dev/null +++ b/app/src/main/java/cn/hugh/bz/gk2/utils/WallpaperServiceHelper.java @@ -0,0 +1,99 @@ +package cn.hugh.bz.gk2.utils; + +import android.content.Context; +import android.content.SharedPreferences; +import android.service.wallpaper.WallpaperService; + +import cn.hugh.bz.gk2.wallpaper.AutoWallpaperService; + +/** + * 壁纸服务状态检测工具类 + * 用于检测 WallpaperService 是否真正在运行 + */ +public class WallpaperServiceHelper { + private static final String TAG = "WallpaperServiceHelper"; + + // SharedPreferences 文件名 + private static final String PREFS_NAME = "wallpaper_service_prefs"; + // 服务运行状态标志 + private static final String KEY_SERVICE_RUNNING = "service_running"; + // 最后活跃时间 + private static final String KEY_LAST_ACTIVE_TIME = "last_active_time"; + + /** + * 当壁纸服务创建时调用(在 WallpaperService.onCreate() 中) + */ + public static void onServiceCreated(Context context) { + SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + prefs.edit() + .putBoolean(KEY_SERVICE_RUNNING, true) + .putLong(KEY_LAST_ACTIVE_TIME, System.currentTimeMillis()) + .apply(); + + FileLogger.getInstance(context).log(TAG, "WallpaperService created, marked as running"); + } + + /** + * 当壁纸服务销毁时调用(在 WallpaperService.onDestroy() 中) + */ + public static void onServiceDestroyed(Context context) { + SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + prefs.edit() + .putBoolean(KEY_SERVICE_RUNNING, false) + .apply(); + + FileLogger.getInstance(context).log(TAG, "WallpaperService destroyed, marked as not running"); + } + + /** + * 更新服务活跃时间(定期调用,例如在 Engine.onCreate() 中) + */ + public static void updateActiveTime(Context context) { + SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + prefs.edit() + .putLong(KEY_LAST_ACTIVE_TIME, System.currentTimeMillis()) + .apply(); + } + + /** + * 检查壁纸服务是否真正在运行 + * @return true 如果服务正在运行且在最近 30 秒内有活动 + */ + public static boolean isServiceRunning(Context context) { + SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + boolean running = prefs.getBoolean(KEY_SERVICE_RUNNING, false); + long lastActiveTime = prefs.getLong(KEY_LAST_ACTIVE_TIME, 0); + + // 如果服务标记为运行,但超过 30 秒没有活动,认为已停止 + long currentTime = System.currentTimeMillis(); + long timeSinceLastActive = currentTime - lastActiveTime; + boolean isActive = running && (timeSinceLastActive < 30000); // 30 秒超时 + + android.util.Log.d(TAG, "isServiceRunning: running=" + running + + ", lastActive=" + timeSinceLastActive + "ms ago, isActive=" + isActive); + + return isActive; + } + + /** + * 检查壁纸服务是否为当前活跃的壁纸 + * 这个方法检查当前设备设置的动态壁纸是否是我们的服务 + */ + public static boolean isCurrentWallpaper(Context context) { + try { + android.app.WallpaperManager wallpaperManager = android.app.WallpaperManager.getInstance(context); + + // 获取当前壁纸信息 + android.content.Intent intent = wallpaperManager.getCropAndSetWallpaperIntent( + android.net.Uri.parse("file://dummy")); + + // 注意:这种方法在某些设备上可能不可靠 + // 更好的方法是结合 isServiceRunning() 来判断 + + return isServiceRunning(context); + } catch (Exception e) { + android.util.Log.e(TAG, "Error checking current wallpaper", e); + return false; + } + } +} diff --git a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java index 7719d3d..8336b5d 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java +++ b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java @@ -18,6 +18,7 @@ import cn.hugh.bz.gk2.model.AppConfig; import cn.hugh.bz.gk2.model.PlayMode; import cn.hugh.bz.gk2.model.WallpaperImage; import cn.hugh.bz.gk2.utils.ImageScanner; +import cn.hugh.bz.gk2.utils.WallpaperServiceHelper; public class AutoWallpaperService extends WallpaperService { private static final String TAG = "AutoWallpaperService"; @@ -28,6 +29,9 @@ public class AutoWallpaperService extends WallpaperService { public void onCreate() { super.onCreate(); + // 标记壁纸服务已创建 + WallpaperServiceHelper.onServiceCreated(this); + // Register refresh receiver android.content.IntentFilter filter = new android.content.IntentFilter("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER_SERVICE"); registerReceiver(refreshReceiver, filter); @@ -36,6 +40,10 @@ public class AutoWallpaperService extends WallpaperService { @Override public void onDestroy() { unregisterReceiver(refreshReceiver); + + // 标记壁纸服务已销毁 + WallpaperServiceHelper.onServiceDestroyed(this); + super.onDestroy(); } @@ -96,6 +104,10 @@ public class AutoWallpaperService extends WallpaperService { @Override public void onCreate(android.view.SurfaceHolder surfaceHolder) { super.onCreate(surfaceHolder); + + // 更新服务活跃时间 + WallpaperServiceHelper.updateActiveTime(AutoWallpaperService.this); + // 标记壁纸已设置 prefs.putBoolean("wallpaper_set", true); } @@ -117,7 +129,9 @@ public class AutoWallpaperService extends WallpaperService { super.onVisibilityChanged(visible); this.visible = visible; + // 当壁纸可见时,更新活跃时间 if (visible) { + WallpaperServiceHelper.updateActiveTime(AutoWallpaperService.this); drawWallpaper(); } else { // Release resources when not visible