feat: 实现真实的壁纸服务运行状态检测
问题:应用重启后显示"运行中",但用户可能还没有设置动态壁纸 解决方案: 1. 创建 WallpaperServiceHelper 工具类来跟踪真实的服务状态 2. 在 WallpaperService 生命周期中更新状态标志 3. 在 HomeFragment 中使用实际运行状态而非配置状态 实现细节: - SharedPreferences 存储服务运行状态和最后活跃时间 - 30 秒超时机制:如果服务超过 30 秒没有活动,认为已停止 - 在 onCreate/onDestroy/onVisibilityChanged 中更新活跃时间 - 三种状态显示: * 服务真正运行:"运行中"(绿色动画) * 配置启用但未设置壁纸:"等待设置壁纸" * 服务已禁用:"已禁用" Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e67ced049d
commit
6bbd0cb59c
3 files changed
+132
-2
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user