剔除服务锁,正在调试RemindThread调用。。。

This commit is contained in:
2025-12-18 15:20:32 +08:00
parent 796d826331
commit d43ba4bff2
3 changed files with 178 additions and 205 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle #Created by .winboll/winboll_app_build.gradle
#Thu Dec 18 05:08:37 GMT 2025 #Thu Dec 18 07:11:20 GMT 2025
stageCount=10 stageCount=10
libraryProject= libraryProject=
baseVersion=15.14 baseVersion=15.14
publishVersion=15.14.9 publishVersion=15.14.9
buildCount=26 buildCount=34
baseBetaVersion=15.14.10 baseBetaVersion=15.14.10

View File

@@ -13,6 +13,7 @@ import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import cc.winboll.studio.libappbase.LogUtils; import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.R; import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.handlers.ControlCenterServiceHandler; import cc.winboll.studio.powerbell.handlers.ControlCenterServiceHandler;
import cc.winboll.studio.powerbell.models.AppConfigBean; import cc.winboll.studio.powerbell.models.AppConfigBean;
@@ -44,29 +45,18 @@ public class ControlCenterService extends Service {
private NotificationManagerUtils mNotificationManager; private NotificationManagerUtils mNotificationManager;
private AppConfigBean mCurrentConfigBean; private AppConfigBean mCurrentConfigBean;
private NotificationMessage mForegroundNotifyMsg; private NotificationMessage mForegroundNotifyMsg;
// 状态标记 // 状态标记(复用 isServiceRunning 替代原 isCoreBusinessStarted
private boolean isServiceRunning; private static volatile boolean isServiceRunning;
private boolean mIsDestroyed; private static volatile boolean mIsDestroyed;
private final Object mServiceLock = new Object();
// ================================== 服务生命周期方法(核心流程入口)================================= // ================================== 服务生命周期方法(核心流程入口)=================================
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
LogUtils.d(TAG, "onCreate: 服务开始创建"); LogUtils.d(TAG, "onCreate: 服务开始创建");
synchronized (mServiceLock) {
isServiceRunning = true; // 调用抽取的核心运行函数
mIsDestroyed = false; runCoreServiceLogic();
// 优先发送前台通知避免5秒超时异常
initForegroundNotificationImmediately();
// 加载本地服务控制配置
mServiceControlBean = ControlCenterServiceBean.loadBean(this, ControlCenterServiceBean.class);
if (mServiceControlBean == null) {
mServiceControlBean = new ControlCenterServiceBean(false);
ControlCenterServiceBean.saveBean(this, mServiceControlBean);
LogUtils.d(TAG, "onCreate: 本地无配置,创建默认禁用配置并持久化");
}
}
LogUtils.d(TAG, "onCreate: 服务创建完成,前台通知已发送,启用状态=" + mServiceControlBean.isEnableService()); LogUtils.d(TAG, "onCreate: 服务创建完成,前台通知已发送,启用状态=" + mServiceControlBean.isEnableService());
} }
@@ -75,34 +65,37 @@ public class ControlCenterService extends Service {
LogUtils.d(TAG, "onStartCommand: 服务启动指令触发"); LogUtils.d(TAG, "onStartCommand: 服务启动指令触发");
// 读取最新控制配置 // 读取最新控制配置
loadLatestServiceControlConfig(); loadLatestServiceControlConfig();
synchronized (mServiceLock) { // 调用抽取的核心运行函数
runCoreServiceLogic();
if (mServiceControlBean.isEnableService()) { if (mServiceControlBean.isEnableService()) {
LogUtils.d(TAG, "onStartCommand: 服务已启用,处理外部指令并初始化业务"); LogUtils.d(TAG, "onStartCommand: 服务已启用,返回START_STICKY");
handleExternalCommand(intent);
initServiceBusinessIfNeed();
return START_STICKY; return START_STICKY;
} else { } else {
LogUtils.d(TAG, "onStartCommand: 服务已禁用,停止所有业务");
stopAllBusinessSafely();
return super.onStartCommand(intent, flags, startId); return super.onStartCommand(intent, flags, startId);
} }
}
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
LogUtils.d(TAG, "onDestroy: 服务开始销毁"); LogUtils.d(TAG, "onDestroy: 服务开始销毁");
synchronized (mServiceLock) {
isServiceRunning = false;
mIsDestroyed = true;
}
// 顺序释放资源 // 顺序释放资源
stopForegroundService(); stopForegroundService();
stopRemindThreadSafely(); stopRemindThreadSafely();
destroyHandler(); destroyHandler();
releaseNotificationResource(); releaseNotificationResource();
clearAllReferences(); clearAllReferences();
// 重置核心业务标记
mCurrentConfigBean = null;
mForegroundNotifyMsg = null;
// 重置核心业务标记
mServiceHandler = null;
isServiceRunning = false;
mIsDestroyed = true;
LogUtils.d(TAG, "onDestroy: 服务销毁完成"); LogUtils.d(TAG, "onDestroy: 服务销毁完成");
} }
@@ -111,6 +104,38 @@ public class ControlCenterService extends Service {
return null; return null;
} }
// ================================== 【核心抽取函数】运行服务核心逻辑 =================================
/**
* 抽取的服务核心运行逻辑统一在onCreate和onStartCommand调用
* 复用 isServiceRunning 做重复运行判断,避免重复初始化业务
*/
private void runCoreServiceLogic() {
loadLatestServiceControlConfig();
// 核心校验:服务已启用 + 服务处于运行状态 + 未被销毁 + 业务未初始化通过mServiceHandler判空
if (mServiceControlBean.isEnableService()) {
if (!isServiceRunning) {
isServiceRunning = true;
mIsDestroyed = false;
LogUtils.d(TAG, "runCoreServiceLogic: 开始执行服务核心业务");
// 初始化通知 + 默认配置 + Handler + 线程
// 优先发送前台通知避免5秒超时异常
initForegroundNotificationImmediately();
// 加载本地服务控制配置
//initNotificationManager();
loadDefaultConfig();
initServiceBusinessLogic();
LogUtils.d(TAG, "runCoreServiceLogic: 核心业务执行完成");
} else {
LogUtils.d(TAG, "runCoreServiceLogic: 核心业务已在运行,无需重复执行");
}
} else {
LogUtils.d(TAG, "runCoreServiceLogic: 服务未启用,不执行核心业务");
}
}
// ================================== 前台通知核心方法(优先执行,防止超时)================================= // ================================== 前台通知核心方法(优先执行,防止超时)=================================
/** /**
* 立即初始化前台通知,内置兜底方案 * 立即初始化前台通知,内置兜底方案
@@ -128,6 +153,7 @@ public class ControlCenterService extends Service {
mForegroundNotifyMsg.setRemindMSG("service_running"); mForegroundNotifyMsg.setRemindMSG("service_running");
} }
mNotificationManager.startForegroundServiceNotify(this, mForegroundNotifyMsg); mNotificationManager.startForegroundServiceNotify(this, mForegroundNotifyMsg);
ToastUtils.show("startForegroundServiceNotify 已调用");
LogUtils.d(TAG, "initForegroundNotificationImmediately: 前台通知发送成功ID=" + NotificationManagerUtils.NOTIFY_ID_FOREGROUND_SERVICE); LogUtils.d(TAG, "initForegroundNotificationImmediately: 前台通知发送成功ID=" + NotificationManagerUtils.NOTIFY_ID_FOREGROUND_SERVICE);
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "initForegroundNotificationImmediately: 工具类发送失败,执行兜底方案", e); LogUtils.e(TAG, "initForegroundNotificationImmediately: 工具类发送失败,执行兜底方案", e);
@@ -159,7 +185,6 @@ public class ControlCenterService extends Service {
*/ */
private void loadLatestServiceControlConfig() { private void loadLatestServiceControlConfig() {
LogUtils.d(TAG, "loadLatestServiceControlConfig: 读取本地配置"); LogUtils.d(TAG, "loadLatestServiceControlConfig: 读取本地配置");
synchronized (mServiceLock) {
ControlCenterServiceBean latestBean = ControlCenterServiceBean.loadBean(this, ControlCenterServiceBean.class); ControlCenterServiceBean latestBean = ControlCenterServiceBean.loadBean(this, ControlCenterServiceBean.class);
if (latestBean != null) { if (latestBean != null) {
mServiceControlBean = latestBean; mServiceControlBean = latestBean;
@@ -168,72 +193,30 @@ public class ControlCenterService extends Service {
LogUtils.w(TAG, "loadLatestServiceControlConfig: 本地无配置,沿用当前状态"); LogUtils.w(TAG, "loadLatestServiceControlConfig: 本地无配置,沿用当前状态");
} }
} }
}
/** /**
* 更新并持久化服务控制配置 * 更新并持久化服务控制配置
*/ */
private void updateAndSaveServiceControlConfig(ControlCenterServiceBean newControlBean) { private void updateAndSaveServiceControlConfig(ControlCenterServiceBean newControlBean) {
if (newControlBean == null) {
LogUtils.e(TAG, "updateAndSaveServiceControlConfig: 配置为空,更新失败");
return;
}
LogUtils.d(TAG, "updateAndSaveServiceControlConfig: 更新配置,启用状态=" + newControlBean.isEnableService());
synchronized (mServiceLock) {
mServiceControlBean = newControlBean;
ControlCenterServiceBean.saveBean(this, mServiceControlBean); ControlCenterServiceBean.saveBean(this, mServiceControlBean);
} }
}
// ================================== 业务初始化与停止(按需加载,避免重复)================================= // ================================== 业务初始化与停止(按需加载,避免重复)=================================
/**
* 按需初始化核心业务
*/
private void initServiceBusinessIfNeed() {
LogUtils.d(TAG, "initServiceBusinessIfNeed: 校验业务初始化条件");
synchronized (mServiceLock) {
if (!mServiceControlBean.isEnableService() || mServiceHandler != null || !isServiceRunning || mIsDestroyed) {
LogUtils.w(TAG, "initServiceBusinessIfNeed: 无需初始化业务");
return;
}
// 初始化通知+配置+业务逻辑
initNotificationManager();
loadDefaultConfig();
initServiceBusinessLogic();
LogUtils.d(TAG, "initServiceBusinessIfNeed: 核心业务初始化完成");
}
}
/**
* 安全停止所有核心业务
*/
private void stopAllBusinessSafely() {
LogUtils.d(TAG, "stopAllBusinessSafely: 停止所有业务");
synchronized (mServiceLock) {
stopRemindThreadSafely();
destroyHandler();
stopForegroundService();
releaseNotificationResource();
mCurrentConfigBean = null;
mForegroundNotifyMsg = null;
}
}
/** /**
* 初始化通知管理工具类 * 初始化通知管理工具类
*/ */
private void initNotificationManager() { // private void initNotificationManager() {
LogUtils.d(TAG, "initNotificationManager: 初始化通知工具"); // LogUtils.d(TAG, "initNotificationManager: 初始化通知工具");
if (mNotificationManager == null) { // if (mNotificationManager == null) {
mNotificationManager = new NotificationManagerUtils(this); // mNotificationManager = new NotificationManagerUtils(this);
} // }
if (mForegroundNotifyMsg == null) { // if (mForegroundNotifyMsg == null) {
mForegroundNotifyMsg = new NotificationMessage(); // mForegroundNotifyMsg = new NotificationMessage();
mForegroundNotifyMsg.setTitle("电池提醒服务运行中"); // mForegroundNotifyMsg.setTitle("电池提醒服务运行中");
mForegroundNotifyMsg.setContent("后台持续监测电池状态"); // mForegroundNotifyMsg.setContent("后台持续监测电池状态");
mForegroundNotifyMsg.setRemindMSG("service_running"); // mForegroundNotifyMsg.setRemindMSG("service_running");
} // }
} // }
/** /**
* 加载默认应用配置 * 加载默认应用配置
@@ -268,17 +251,10 @@ public class ControlCenterService extends Service {
*/ */
public void restartRemindThreadSafely() { public void restartRemindThreadSafely() {
LogUtils.d(TAG, "restartRemindThreadSafely: 开始重启线程"); LogUtils.d(TAG, "restartRemindThreadSafely: 开始重启线程");
synchronized (mServiceLock) {
if (!isServiceRunning || mIsDestroyed || !mServiceControlBean.isEnableService()) {
LogUtils.e(TAG, "restartRemindThreadSafely: 服务状态异常,启动失败");
return;
}
// 停止旧线程 // 停止旧线程
stopRemindThreadSafely(); stopRemindThreadSafely();
if (mServiceHandler == null) { if (mServiceHandler == null) {
LogUtils.e(TAG, "restartRemindThreadSafely: Handler为空启动失败");
return;
}
// 创建新线程实例 // 创建新线程实例
RemindThread.destroyInstance(); RemindThread.destroyInstance();
mRemindThread = RemindThread.getInstance(this, mServiceHandler); mRemindThread = RemindThread.getInstance(this, mServiceHandler);
@@ -295,7 +271,10 @@ public class ControlCenterService extends Service {
} else { } else {
LogUtils.w(TAG, "restartRemindThreadSafely: 线程状态异常,跳过启动"); LogUtils.w(TAG, "restartRemindThreadSafely: 线程状态异常,跳过启动");
} }
} else {
LogUtils.d(TAG, "mServiceHandler is null");
} }
} }
/** /**
@@ -335,7 +314,6 @@ public class ControlCenterService extends Service {
*/ */
private void syncConfigToRemindThread() { private void syncConfigToRemindThread() {
LogUtils.d(TAG, "syncConfigToRemindThread: 同步配置"); LogUtils.d(TAG, "syncConfigToRemindThread: 同步配置");
synchronized (mServiceLock) {
if (mRemindThread == null || mCurrentConfigBean == null) { if (mRemindThread == null || mCurrentConfigBean == null) {
LogUtils.e(TAG, "syncConfigToRemindThread: 线程或配置为空,同步失败"); LogUtils.e(TAG, "syncConfigToRemindThread: 线程或配置为空,同步失败");
return; return;
@@ -343,36 +321,36 @@ public class ControlCenterService extends Service {
mRemindThread.setAppConfigBean(mCurrentConfigBean); mRemindThread.setAppConfigBean(mCurrentConfigBean);
mRemindThread.setIsReminding(true); mRemindThread.setIsReminding(true);
} }
}
// ================================== 外部指令处理接收Intent指令================================= // ================================== 外部指令处理接收Intent指令=================================
/** /**
* 处理外部发送的服务指令 * 处理外部发送的服务指令
*/ */
private void handleExternalCommand(Intent intent) { // private void handleExternalCommand(Intent intent) {
LogUtils.d(TAG, "handleExternalCommand: 处理外部指令"); // LogUtils.d(TAG, "handleExternalCommand: 处理外部指令");
if (intent == null || TextUtils.isEmpty(intent.getAction())) { // if (intent == null || TextUtils.isEmpty(intent.getAction())) {
LogUtils.e(TAG, "handleExternalCommand: Intent或Action为空"); // LogUtils.e(TAG, "handleExternalCommand: Intent或Action为空");
return; // return;
} // }
String action = intent.getAction(); // String action = intent.getAction();
if (ACTION_RESTART_REMIND_THREAD.equals(action)) { // if (ACTION_RESTART_REMIND_THREAD.equals(action)) {
synchronized (mServiceLock) { // synchronized (mServiceLock) {
if (mServiceControlBean.isEnableService()) { // if (mServiceControlBean.isEnableService()) {
AppConfigBean newConfig = (AppConfigBean) intent.getSerializableExtra(EXTRA_APP_CONFIG_BEAN); // AppConfigBean newConfig = (AppConfigBean) intent.getSerializableExtra(EXTRA_APP_CONFIG_BEAN);
if (newConfig != null) { // if (newConfig != null) {
mCurrentConfigBean = newConfig; // mCurrentConfigBean = newConfig;
LogUtils.d(TAG, "handleExternalCommand: 收到配置更新,重启线程"); // LogUtils.d(TAG, "handleExternalCommand: 收到配置更新,重启线程");
restartRemindThreadSafely(); // restartRemindThreadSafely();
} else { // } else {
LogUtils.e(TAG, "handleExternalCommand: 配置为空,重启失败"); // LogUtils.e(TAG, "handleExternalCommand: 配置为空,重启失败");
} // }
} else { // } else {
LogUtils.w(TAG, "handleExternalCommand: 服务已禁用,忽略指令"); // LogUtils.w(TAG, "handleExternalCommand: 服务已禁用,忽略指令");
} // }
} // }
} // }
} // }
// ================================== 资源释放方法(防止内存泄漏)================================= // ================================== 资源释放方法(防止内存泄漏)=================================
/** /**
@@ -432,11 +410,17 @@ public class ControlCenterService extends Service {
LogUtils.e(TAG, "startControlCenterService: Context为空"); LogUtils.e(TAG, "startControlCenterService: Context为空");
return; return;
} }
// 保存服务启动配置
ControlCenterServiceBean.saveBean(context, new ControlCenterServiceBean(true));
Intent intent = new Intent(context, ControlCenterService.class); Intent intent = new Intent(context, ControlCenterService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent); context.startForegroundService(intent);
LogUtils.d(TAG, "startForegroundService 已调用");
} else { } else {
context.startService(intent); context.startService(intent);
LogUtils.d(TAG, "startService 已调用");
} }
} }
@@ -449,6 +433,10 @@ public class ControlCenterService extends Service {
LogUtils.e(TAG, "stopControlCenterService: Context为空"); LogUtils.e(TAG, "stopControlCenterService: Context为空");
return; return;
} }
// 保存服务启动配置
ControlCenterServiceBean.saveBean(context, new ControlCenterServiceBean(false));
Intent intent = new Intent(context, ControlCenterService.class); Intent intent = new Intent(context, ControlCenterService.class);
context.stopService(intent); context.stopService(intent);
} }
@@ -456,20 +444,18 @@ public class ControlCenterService extends Service {
/** /**
* 更新服务控制配置并启停服务 * 更新服务控制配置并启停服务
*/ */
public static void updateServiceControlConfig(Context context, ControlCenterServiceBean controlBean) { // public static void updateServiceControlConfig(Context context, ControlCenterServiceBean controlBean) {
LogUtils.d(TAG, "updateServiceControlConfig: 更新控制配置,启用状态=" + controlBean.isEnableService()); // LogUtils.d(TAG, "updateServiceControlConfig: 更新控制配置,启用状态=" + controlBean.isEnableService());
if (context == null || controlBean == null) { // if (context == null || controlBean == null) {
LogUtils.e(TAG, "updateServiceControlConfig: 参数为空"); // LogUtils.e(TAG, "updateServiceControlConfig: 参数为空");
return; // return;
} // }
ControlCenterServiceBean.saveBean(context, controlBean); // if (controlBean.isEnableService()) {
if (controlBean.isEnableService()) { // startControlCenterService(context);
stopControlCenterService(context); // } else {
startControlCenterService(context); // stopControlCenterService(context);
} else { // }
stopControlCenterService(context); // }
}
}
/** /**
* 更新业务配置并触发线程重启 * 更新业务配置并触发线程重启
@@ -577,10 +563,8 @@ public class ControlCenterService extends Service {
// ================================== Getter/Setter 方法(线程安全)================================= // ================================== Getter/Setter 方法(线程安全)=================================
public RemindThread getRemindThread() { public RemindThread getRemindThread() {
synchronized (mServiceLock) {
return mRemindThread; return mRemindThread;
} }
}
public void setCurrentConfigBean(AppConfigBean configBean) { public void setCurrentConfigBean(AppConfigBean configBean) {
LogUtils.d(TAG, "setCurrentConfigBean: 更新业务配置"); LogUtils.d(TAG, "setCurrentConfigBean: 更新业务配置");
@@ -588,47 +572,35 @@ public class ControlCenterService extends Service {
LogUtils.e(TAG, "setCurrentConfigBean: 配置为空"); LogUtils.e(TAG, "setCurrentConfigBean: 配置为空");
return; return;
} }
synchronized (mServiceLock) {
this.mCurrentConfigBean = configBean; this.mCurrentConfigBean = configBean;
syncConfigToRemindThread(); syncConfigToRemindThread();
} }
}
public void setServiceControlBean(ControlCenterServiceBean controlBean) { // public void setServiceControlBean(ControlCenterServiceBean controlBean) {
LogUtils.d(TAG, "setServiceControlBean: 更新控制配置"); // LogUtils.d(TAG, "setServiceControlBean: 更新控制配置");
if (controlBean != null) { // if (controlBean != null) {
updateAndSaveServiceControlConfig(controlBean); // updateAndSaveServiceControlConfig(controlBean);
} // }
} // }
public ControlCenterServiceBean getServiceControlBean() { public ControlCenterServiceBean getServiceControlBean() {
synchronized (mServiceLock) {
return mServiceControlBean; return mServiceControlBean;
} }
}
public NotificationManagerUtils getNotificationManager() { public NotificationManagerUtils getNotificationManager() {
synchronized (mServiceLock) {
return mNotificationManager; return mNotificationManager;
} }
}
public NotificationMessage getForegroundNotifyMsg() { public NotificationMessage getForegroundNotifyMsg() {
synchronized (mServiceLock) {
return mForegroundNotifyMsg; return mForegroundNotifyMsg;
} }
}
public AppConfigBean getCurrentConfigBean() { public AppConfigBean getCurrentConfigBean() {
synchronized (mServiceLock) {
return mCurrentConfigBean; return mCurrentConfigBean;
} }
}
public boolean isDestroyed() { public boolean isDestroyed() {
synchronized (mServiceLock) {
return mIsDestroyed; return mIsDestroyed;
} }
}
} }

View File

@@ -602,11 +602,12 @@ public class MainContentView {
break; break;
// 服务总开关(核心:持久化配置+触发 Activity 回调) // 服务总开关(核心:持久化配置+触发 Activity 回调)
case CHANGE_TYPE_SERVICE_SWITCH: case CHANGE_TYPE_SERVICE_SWITCH:
// 1. 保存服务控制配置到本地 // 1. 设置服务启停
ControlCenterServiceBean serviceBean = ControlCenterServiceBean.loadBean(mContext, ControlCenterServiceBean.class); if (mTempConfigData.newBooleanValue) {
if (serviceBean == null) serviceBean = new ControlCenterServiceBean(false); ControlCenterService.startControlCenterService(mContext);
serviceBean.setIsEnableService(mTempConfigData.newBooleanValue); } else {
ControlCenterService.updateServiceControlConfig(mContext, serviceBean); ControlCenterService.stopControlCenterService(mContext);
}
// 2. 强制触发 Activity 回调,执行服务启停逻辑 // 2. 强制触发 Activity 回调,执行服务启停逻辑
mActionListener.onServiceSwitchChanged(mTempConfigData.newBooleanValue); mActionListener.onServiceSwitchChanged(mTempConfigData.newBooleanValue);
LogUtils.d(TAG, "confirmConfigChange: 服务开关确认,值=" + mTempConfigData.newBooleanValue + ",已持久化配置"); LogUtils.d(TAG, "confirmConfigChange: 服务开关确认,值=" + mTempConfigData.newBooleanValue + ",已持久化配置");