修复广播消息发送方法

This commit is contained in:
2025-12-20 15:55:40 +08:00
parent e539922478
commit 6af2096b30
3 changed files with 28 additions and 22 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle #Created by .winboll/winboll_app_build.gradle
#Sat Dec 20 07:20:03 GMT 2025 #Sat Dec 20 07:53:57 GMT 2025
stageCount=10 stageCount=10
libraryProject= libraryProject=
baseVersion=15.14 baseVersion=15.14
publishVersion=15.14.9 publishVersion=15.14.9
buildCount=58 buildCount=61
baseBetaVersion=15.14.10 baseBetaVersion=15.14.10

View File

@@ -40,6 +40,7 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
// ================================== 成员变量区(弱引用防泄漏,按功能分层)================================= // ================================== 成员变量区(弱引用防泄漏,按功能分层)=================================
private WeakReference<ControlCenterService> mwrControlCenterService; private WeakReference<ControlCenterService> mwrControlCenterService;
private boolean isRegistered = false; // 新增:标记广播注册状态,避免冗余操作
// ================================== 构造方法(初始化弱引用,避免服务强引用泄漏)================================= // ================================== 构造方法(初始化弱引用,避免服务强引用泄漏)=================================
public ControlCenterServiceReceiver(ControlCenterService service) { public ControlCenterServiceReceiver(ControlCenterService service) {
@@ -76,6 +77,7 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
handleUpdateForegroundNotification(service); handleUpdateForegroundNotification(service);
break; break;
case ACTION_APPCONFIG_CHANGED: case ACTION_APPCONFIG_CHANGED:
LogUtils.d(TAG, "onReceive: 开始处理配置更新广播"); // 新增:标记配置广播处理起点
handleNotifyAppConfigUpdate(service); handleNotifyAppConfigUpdate(service);
break; break;
default: default:
@@ -128,6 +130,10 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
try { try {
// 加载最新配置 // 加载最新配置
AppConfigBean latestConfig = AppConfigUtils.getInstance(service).loadAppConfig(); AppConfigBean latestConfig = AppConfigUtils.getInstance(service).loadAppConfig();
if (latestConfig == null) { // 新增:配置空指针防护
LogUtils.e(TAG, "handleNotifyAppConfigUpdate: 最新配置为空,终止处理");
return;
}
LogUtils.d(TAG, "handleNotifyAppConfigUpdate: 加载最新配置 | 充电阈值=" + latestConfig.getChargeReminderValue() + " | 耗电阈值=" + latestConfig.getUsageReminderValue()); LogUtils.d(TAG, "handleNotifyAppConfigUpdate: 加载最新配置 | 充电阈值=" + latestConfig.getChargeReminderValue() + " | 耗电阈值=" + latestConfig.getUsageReminderValue());
// 同步缓存的电池状态到配置 // 同步缓存的电池状态到配置
@@ -136,6 +142,7 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
service.notifyAppConfigUpdate(latestConfig); service.notifyAppConfigUpdate(latestConfig);
LogUtils.d(TAG, "handleNotifyAppConfigUpdate: 配置同步成功 | 缓存电量=" + sLastBatteryLevel + "% | 充电状态=" + sIsCharging); LogUtils.d(TAG, "handleNotifyAppConfigUpdate: 配置同步成功 | 缓存电量=" + sLastBatteryLevel + "% | 充电状态=" + sIsCharging);
LogUtils.d(TAG, "handleNotifyAppConfigUpdate: 配置更新广播处理完成"); // 新增:标记配置广播处理终点
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "handleNotifyAppConfigUpdate: 处理失败", e); LogUtils.e(TAG, "handleNotifyAppConfigUpdate: 处理失败", e);
} }
@@ -171,15 +178,12 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
*/ */
public void registerAction(Context context) { public void registerAction(Context context) {
LogUtils.d(TAG, "registerAction: 注册广播接收器 | context=" + context); LogUtils.d(TAG, "registerAction: 注册广播接收器 | context=" + context);
if (context == null) { if (context == null || isRegistered) { // 新增:已注册则跳过
LogUtils.e(TAG, "registerAction: 上下文为空,注册失败"); LogUtils.e(TAG, "registerAction: 上下文为空或已注册,注册失败");
return; return;
} }
try { try {
// 先注销再注册,避免重复注册异常
unregisterAction(context);
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED); filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(ACTION_UPDATE_FOREGROUND_NOTIFICATION); filter.addAction(ACTION_UPDATE_FOREGROUND_NOTIFICATION);
@@ -187,6 +191,7 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
filter.setPriority(BROADCAST_PRIORITY); filter.setPriority(BROADCAST_PRIORITY);
context.registerReceiver(this, filter); context.registerReceiver(this, filter);
isRegistered = true; // 标记为已注册
LogUtils.d(TAG, "registerAction: 广播注册成功 | 优先级=" + BROADCAST_PRIORITY); LogUtils.d(TAG, "registerAction: 广播注册成功 | 优先级=" + BROADCAST_PRIORITY);
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "registerAction: 注册失败", e); LogUtils.e(TAG, "registerAction: 注册失败", e);
@@ -199,13 +204,14 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
*/ */
public void unregisterAction(Context context) { public void unregisterAction(Context context) {
LogUtils.d(TAG, "unregisterAction: 注销广播接收器 | context=" + context); LogUtils.d(TAG, "unregisterAction: 注销广播接收器 | context=" + context);
if (context == null) { if (context == null || !isRegistered) { // 新增:未注册则跳过
LogUtils.e(TAG, "unregisterAction: 上下文为空,注销失败"); LogUtils.e(TAG, "unregisterAction: 上下文为空或未注册,注销失败");
return; return;
} }
try { try {
context.unregisterReceiver(this); context.unregisterReceiver(this);
isRegistered = false; // 标记为未注册
LogUtils.d(TAG, "unregisterAction: 广播注销成功"); LogUtils.d(TAG, "unregisterAction: 广播注销成功");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LogUtils.w(TAG, "unregisterAction: 广播未注册,跳过注销"); LogUtils.w(TAG, "unregisterAction: 广播未注册,跳过注销");
@@ -248,4 +254,3 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
return sIsCharging; return sIsCharging;
} }
} }

View File

@@ -337,19 +337,20 @@ public class ControlCenterService extends Service {
* 外部更新配置并触发线程重启 * 外部更新配置并触发线程重启
* @param context 上下文 * @param context 上下文
*/ */
public static void sendAppConfigStatusUpdateMessage(Context context) { public static void sendAppConfigStatusUpdateMessage(Context context) {
LogUtils.d(TAG, "sendAppConfigStatusUpdateMessage执行 | context=" + context); LogUtils.d(TAG, "sendAppConfigStatusUpdateMessage执行 | context=" + context);
if (context == null) { if (context == null) {
LogUtils.e(TAG, "sendAppConfigStatusUpdateMessage参数为空更新失败"); LogUtils.e(TAG, "sendAppConfigStatusUpdateMessage参数为空更新失败");
return; return;
} }
Intent intent = new Intent(context, ControlCenterServiceReceiver.class); Intent intent = new Intent(ControlCenterServiceReceiver.ACTION_APPCONFIG_CHANGED);
intent.setAction(ControlCenterServiceReceiver.ACTION_APPCONFIG_CHANGED); intent.setPackage(context.getPackageName());
intent.setPackage(context.getPackageName()); // 新增:发送广播并记录结果
context.sendBroadcast(intent); context.sendBroadcast(intent);
LogUtils.d(TAG, "sendAppConfigStatusUpdateMessage配置更新广播发送 | action=" + ControlCenterServiceReceiver.ACTION_APPCONFIG_CHANGED); LogUtils.d(TAG, "sendAppConfigStatusUpdateMessage配置更新广播发送 action=" + ControlCenterServiceReceiver.ACTION_APPCONFIG_CHANGED);
} }
/** /**
* 检查并引导用户开启忽略电池优化API23+ * 检查并引导用户开启忽略电池优化API23+