添加广播接收器的注册与释放。
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Sat Dec 20 05:22:35 GMT 2025
|
||||
#Sat Dec 20 05:46:25 GMT 2025
|
||||
stageCount=10
|
||||
libraryProject=
|
||||
baseVersion=15.14
|
||||
publishVersion=15.14.9
|
||||
buildCount=51
|
||||
buildCount=54
|
||||
baseBetaVersion=15.14.10
|
||||
|
||||
@@ -22,11 +22,11 @@ import java.lang.ref.WeakReference;
|
||||
*/
|
||||
public class ControlCenterServiceReceiver extends BroadcastReceiver {
|
||||
// ================================== 静态常量区(置顶归类,消除魔法值)=================================
|
||||
public static final String TAG = ControlCenterServiceReceiver.class.getSimpleName();
|
||||
public static final String TAG = "ControlCenterServiceReceiver";
|
||||
|
||||
// 广播Action常量(带包名前缀防冲突)
|
||||
public static final String ACTION_UPDATE_FOREGROUND_NOTIFICATION = "cc.winboll.studio.powerbell.action.ACTION_UPDATE_SERVICE_FOREGROUND_NOTIFICATION";
|
||||
public static final String ACTION_APPCONFIG_CHANGED = "cc.winboll.studio.powerbell.action.ACTION_START_REMIND__NOTIFICATION";
|
||||
public static final String ACTION_UPDATE_FOREGROUND_NOTIFICATION = "cc.winboll.studio.powerbell.action.ACTION_UPDATE_FOREGROUND_NOTIFICATION";
|
||||
public static final String ACTION_APPCONFIG_CHANGED = "cc.winboll.studio.powerbell.action.ACTION_APPCONFIG_CHANGED";
|
||||
public static final String EXTRA_APP_CONFIG_BEAN = "extra_app_config_bean";
|
||||
|
||||
// 广播优先级与电量范围常量
|
||||
@@ -129,7 +129,7 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
|
||||
private void handleNotifyAppConfigUpdate(ControlCenterService service) {
|
||||
LogUtils.d(TAG, "handleNotifyAppConfigUpdate: 同步缓存状态到配置 | service=" + service);
|
||||
try {
|
||||
// 加载最新配置(三级兜底)
|
||||
// 加载最新配置
|
||||
AppConfigBean latestConfig = AppConfigUtils.getInstance(service).loadAppConfig();
|
||||
|
||||
// 同步缓存的电池状态到配置
|
||||
|
||||
@@ -9,7 +9,6 @@ import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
import java.util.List;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
import cc.winboll.studio.powerbell.handlers.ControlCenterServiceHandler;
|
||||
@@ -19,6 +18,7 @@ import cc.winboll.studio.powerbell.models.NotificationMessage;
|
||||
import cc.winboll.studio.powerbell.receivers.ControlCenterServiceReceiver;
|
||||
import cc.winboll.studio.powerbell.threads.RemindThread;
|
||||
import cc.winboll.studio.powerbell.utils.NotificationManagerUtils;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 电池提醒核心服务
|
||||
@@ -31,7 +31,7 @@ public class ControlCenterService extends Service {
|
||||
|
||||
// 超时/阈值常量
|
||||
private static final long THREAD_STOP_TIMEOUT = 1000L;
|
||||
// 服务状态标记常量
|
||||
// 服务返回策略常量
|
||||
private static final int SERVICE_RETURN_STICKY = START_STICKY;
|
||||
|
||||
// ================================== 成员变量区(按功能分层,volatile保证多线程可见性)=================================
|
||||
@@ -39,6 +39,7 @@ public class ControlCenterService extends Service {
|
||||
private ControlCenterServiceBean mServiceControlBean;
|
||||
// 业务核心组件
|
||||
private ControlCenterServiceHandler mServiceHandler;
|
||||
private ControlCenterServiceReceiver mControlCenterServiceReceiver;
|
||||
private NotificationManagerUtils mNotificationManager;
|
||||
private AppConfigBean mCurrentConfigBean;
|
||||
private NotificationMessage mForegroundNotifyMsg;
|
||||
@@ -82,9 +83,13 @@ public class ControlCenterService extends Service {
|
||||
LogUtils.d(TAG, "onDestroy: 服务销毁流程启动");
|
||||
super.onDestroy();
|
||||
|
||||
// 资源释放顺序:前台服务 → 线程 → Handler → 通知 → 引用(避免内存泄漏)
|
||||
// 资源释放顺序:前台服务 → 线程 → 广播接收器 → Handler → 通知 → 引用(避免内存泄漏)
|
||||
stopForegroundService();
|
||||
RemindThread.stopRemindThreadSafely();
|
||||
if (mControlCenterServiceReceiver != null) {
|
||||
mControlCenterServiceReceiver.release();
|
||||
LogUtils.d(TAG, "onDestroy: 广播接收器已释放");
|
||||
}
|
||||
destroyHandler();
|
||||
releaseNotificationResource();
|
||||
clearAllReferences();
|
||||
@@ -200,7 +205,7 @@ public class ControlCenterService extends Service {
|
||||
mCurrentConfigBean.setEnableUsageReminder(true);
|
||||
mCurrentConfigBean.setUsageReminderValue(20);
|
||||
mCurrentConfigBean.setBatteryDetectInterval(1000);
|
||||
LogUtils.d(TAG, "loadDefaultConfig: 默认配置加载完成 | 充电阈值=80 | 耗电阈值=20");
|
||||
LogUtils.d(TAG, "loadDefaultConfig: 默认配置加载完成 | 充电阈值=80 | 耗电阈值=20 | 检测间隔=1000ms");
|
||||
} else {
|
||||
LogUtils.d(TAG, "loadDefaultConfig: 内存已有配置,无需加载");
|
||||
}
|
||||
@@ -212,12 +217,21 @@ public class ControlCenterService extends Service {
|
||||
*/
|
||||
private void initServiceBusinessLogic() {
|
||||
LogUtils.d(TAG, "initServiceBusinessLogic: 初始化业务组件");
|
||||
// 初始化Handler
|
||||
if (mServiceHandler == null) {
|
||||
mServiceHandler = new ControlCenterServiceHandler(this);
|
||||
LogUtils.d(TAG, "initServiceBusinessLogic: Handler初始化完成");
|
||||
} else {
|
||||
LogUtils.d(TAG, "initServiceBusinessLogic: Handler已存在");
|
||||
}
|
||||
// 初始化广播接收器
|
||||
if (mControlCenterServiceReceiver == null) {
|
||||
mControlCenterServiceReceiver = new ControlCenterServiceReceiver(this);
|
||||
mControlCenterServiceReceiver.registerAction(this);
|
||||
LogUtils.d(TAG, "initServiceBusinessLogic: 广播接收器初始化并注册完成");
|
||||
} else {
|
||||
LogUtils.d(TAG, "initServiceBusinessLogic: 广播接收器已存在");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,15 +287,16 @@ public class ControlCenterService extends Service {
|
||||
// 保存启用配置
|
||||
ControlCenterServiceBean controlBean = new ControlCenterServiceBean(true);
|
||||
ControlCenterServiceBean.saveBean(context, controlBean);
|
||||
LogUtils.d(TAG, "startControlCenterService: 服务启用配置已保存");
|
||||
|
||||
// 启动服务(区分API版本)
|
||||
Intent intent = new Intent(context, ControlCenterService.class);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(intent);
|
||||
LogUtils.d(TAG, "startControlCenterService: 以前台服务方式启动");
|
||||
LogUtils.d(TAG, "startControlCenterService: 以前台服务方式启动(API26+)");
|
||||
} else {
|
||||
context.startService(intent);
|
||||
LogUtils.d(TAG, "startControlCenterService: 以普通服务方式启动");
|
||||
LogUtils.d(TAG, "startControlCenterService: 以普通服务方式启动(API26-)");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,6 +314,7 @@ public class ControlCenterService extends Service {
|
||||
// 保存停用配置
|
||||
ControlCenterServiceBean controlBean = new ControlCenterServiceBean(false);
|
||||
ControlCenterServiceBean.saveBean(context, controlBean);
|
||||
LogUtils.d(TAG, "stopControlCenterService: 服务停用配置已保存");
|
||||
|
||||
// 停止服务
|
||||
Intent intent = new Intent(context, ControlCenterService.class);
|
||||
@@ -331,7 +347,7 @@ public class ControlCenterService extends Service {
|
||||
public static void checkIgnoreBatteryOptimization(Context context) {
|
||||
LogUtils.d(TAG, "checkIgnoreBatteryOptimization: 检查电池优化 | context=" + context);
|
||||
if (context == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
||||
LogUtils.w(TAG, "checkIgnoreBatteryOptimization: 无需检查");
|
||||
LogUtils.w(TAG, "checkIgnoreBatteryOptimization: 无需检查(Context为空或API<23)");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -361,7 +377,7 @@ public class ControlCenterService extends Service {
|
||||
* @return true=运行中 false=未运行
|
||||
*/
|
||||
private static boolean isServiceRunning(Context context, Class<?> serviceClass) {
|
||||
LogUtils.d(TAG, "isServiceRunning: 检查服务状态 | context=" + context + " | service=" + serviceClass.getName());
|
||||
LogUtils.d(TAG, "isServiceRunning: 检查服务状态 | context=" + context + " | service=" + (serviceClass != null ? serviceClass.getName() : "null"));
|
||||
if (context == null || serviceClass == null) {
|
||||
LogUtils.e(TAG, "isServiceRunning: 参数为空");
|
||||
return false;
|
||||
@@ -437,7 +453,7 @@ public class ControlCenterService extends Service {
|
||||
if (latestConfig != null && mServiceHandler != null) {
|
||||
mCurrentConfigBean = latestConfig;
|
||||
RemindThread.startRemindThread(this, mServiceHandler, latestConfig);
|
||||
LogUtils.d(TAG, "notifyAppConfigUpdate: 配置已同步到提醒线程");
|
||||
LogUtils.d(TAG, "notifyAppConfigUpdate: 配置已同步到提醒线程 | 充电阈值=" + latestConfig.getChargeReminderValue() + " | 耗电阈值=" + latestConfig.getUsageReminderValue());
|
||||
} else {
|
||||
LogUtils.e(TAG, "notifyAppConfigUpdate: 参数为空,同步失败 | latestConfig=" + latestConfig + " | mServiceHandler=" + mServiceHandler);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user