线程提醒电量问题调试完成

This commit is contained in:
2025-12-24 21:07:54 +08:00
parent 80da7677f8
commit f4a485f1ff
5 changed files with 60 additions and 57 deletions

View File

@@ -42,7 +42,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
int reminderIntervalTime = 5000; // 铃声提醒间隔ms
// 电量状态
boolean isCharging = false; // 是否充电
int currentBatteryValue = INVALID_BATTERY; // 当前电池电量(统一命名,替代原 currentValue
//int currentBatteryValue = INVALID_BATTERY; // 当前电池电量(统一命名,替代原 currentValue
// 电量检测配置
int batteryDetectInterval = 2000; // 电量检测间隔ms适配 RemindThread
// 相框配置
@@ -57,7 +57,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
setEnableUsageReminder(false);
setReminderIntervalTime(5000);
setBatteryDetectInterval(1000); // 默认检测间隔1秒
setCurrentBatteryValue(INVALID_BATTERY); // 初始化无效电量标识
//setCurrentBatteryValue(INVALID_BATTERY); // 初始化无效电量标识
setDefaultFrameWidth(DEFAULT_FRAME_WIDTH); // 初始化默认相框宽度
setDefaultFrameHeight(DEFAULT_FRAME_HEIGHT); // 初始化默认相框高度
LogUtils.d(TAG, "AppConfigBean() 初始化默认配置完成");
@@ -69,20 +69,20 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
* 设置当前电池电量Receiver 监听电池变化时调用)
* @param currentBatteryValue 当前电量0-100
*/
public void setCurrentBatteryValue(int currentBatteryValue) {
this.currentBatteryValue = (currentBatteryValue >= BATTERY_MIN && currentBatteryValue <= BATTERY_MAX)
? currentBatteryValue : INVALID_BATTERY;
LogUtils.d(TAG, String.format("setCurrentBatteryValue() 执行 | 最终电量=%d | 输入值=%d",
this.currentBatteryValue, currentBatteryValue));
}
/**
* 获取当前电池电量RemindThread 同步配置时调用)
* @return 当前电量0-100 或 INVALID_BATTERY
*/
public int getCurrentBatteryValue() {
return currentBatteryValue;
}
// public void setCurrentBatteryValue(int currentBatteryValue) {
// this.currentBatteryValue = (currentBatteryValue >= BATTERY_MIN && currentBatteryValue <= BATTERY_MAX)
// ? currentBatteryValue : INVALID_BATTERY;
// LogUtils.d(TAG, String.format("setCurrentBatteryValue() 执行 | 最终电量=%d | 输入值=%d",
// this.currentBatteryValue, currentBatteryValue));
// }
//
// /**
// * 获取当前电池电量RemindThread 同步配置时调用)
// * @return 当前电量0-100 或 INVALID_BATTERY
// */
// public int getCurrentBatteryValue() {
// return currentBatteryValue;
// }
// --------------- 铃声提醒配置相关 ---------------
/**
@@ -222,8 +222,8 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
jsonWriter.name("reminderIntervalTime").value(getReminderIntervalTime());
jsonWriter.name("isCharging").value(isCharging());
// 兼容旧字段 currentValue同步新字段 currentBatteryValue
jsonWriter.name("currentBatteryValue").value(getCurrentBatteryValue());
jsonWriter.name("currentValue").value(getCurrentBatteryValue());
// jsonWriter.name("currentBatteryValue").value(getCurrentBatteryValue());
// jsonWriter.name("currentValue").value(getCurrentBatteryValue());
// 新增字段序列化(检测配置)
jsonWriter.name("batteryDetectInterval").value(getBatteryDetectInterval());
// 新增字段序列化(相框配置)
@@ -257,15 +257,17 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
} else if (name.equals("isCharging")) {
bean.setIsCharging(jsonReader.nextBoolean());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%b", name, bean.isCharging()));
} else if (name.equals("currentValue")) {
// 优先读取旧字段,兼容历史配置
bean.setCurrentBatteryValue(jsonReader.nextInt());
LogUtils.d(TAG, "readBeanFromJsonReader() 读取旧字段 | currentValue=" + bean.getCurrentBatteryValue());
} else if (name.equals("currentBatteryValue")) {
// 新字段覆盖旧字段,保证数据最新
bean.setCurrentBatteryValue(jsonReader.nextInt());
LogUtils.d(TAG, "readBeanFromJsonReader() 读取新字段 | currentBatteryValue=" + bean.getCurrentBatteryValue());
} else if (name.equals("batteryDetectInterval")) {
}
// else if (name.equals("currentValue")) {
// // 优先读取旧字段,兼容历史配置
// bean.setCurrentBatteryValue(jsonReader.nextInt());
// LogUtils.d(TAG, "readBeanFromJsonReader() 读取旧字段 | currentValue=" + bean.getCurrentBatteryValue());
// } else if (name.equals("currentBatteryValue")) {
// // 新字段覆盖旧字段,保证数据最新
// bean.setCurrentBatteryValue(jsonReader.nextInt());
// LogUtils.d(TAG, "readBeanFromJsonReader() 读取新字段 | currentBatteryValue=" + bean.getCurrentBatteryValue());
// }
else if (name.equals("batteryDetectInterval")) {
bean.setBatteryDetectInterval(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getBatteryDetectInterval()));
} else if (name.equals("defaultFrameWidth")) {
@@ -299,7 +301,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
dest.writeInt(chargeReminderValue);
dest.writeInt(reminderIntervalTime);
dest.writeByte((byte) (isCharging ? 1 : 0));
dest.writeInt(currentBatteryValue);
//dest.writeInt(currentBatteryValue);
dest.writeInt(batteryDetectInterval);
dest.writeInt(defaultFrameWidth);
dest.writeInt(defaultFrameHeight);
@@ -318,7 +320,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
bean.chargeReminderValue = source.readInt();
bean.reminderIntervalTime = source.readInt();
bean.isCharging = source.readByte() != 0;
bean.currentBatteryValue = source.readInt();
//bean.currentBatteryValue = source.readInt();
bean.batteryDetectInterval = source.readInt();
bean.defaultFrameWidth = source.readInt();
bean.defaultFrameHeight = source.readInt();

View File

@@ -8,6 +8,7 @@ import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.models.AppConfigBean;
import cc.winboll.studio.powerbell.models.NotificationMessage;
import cc.winboll.studio.powerbell.services.ControlCenterService;
import cc.winboll.studio.powerbell.threads.RemindThread;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.BatteryUtils;
import cc.winboll.studio.powerbell.utils.NotificationManagerUtils;
@@ -138,7 +139,7 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
latestConfig.getChargeReminderValue(), latestConfig.getUsageReminderValue()));
// 同步缓存的电池状态到配置
latestConfig.setCurrentBatteryValue(sLastBatteryLevel);
RemindThread.sQuantityOfElectricity = sLastBatteryLevel;
latestConfig.setIsCharging(sIsCharging);
service.notifyAppConfigUpdate(latestConfig);

View File

@@ -55,7 +55,7 @@ public class RemindThread extends Thread {
private volatile long sleepTime;
private volatile int chargeReminderValue;
private volatile int usageReminderValue;
private volatile int quantityOfElectricity;
public static volatile int sQuantityOfElectricity = INVALID_BATTERY_VALUE;;
private volatile boolean isCharging;
// ====================== 私有构造器(禁止外部实例化) ======================
@@ -117,7 +117,7 @@ public class RemindThread extends Thread {
* @param batteryLevel 当前电量
* @return true: 启动成功false: 入参非法
*/
public static boolean startRemindThreadWithBatteryInfo(Context context, ControlCenterServiceHandler handler, boolean isCharging, int batteryLevel) {
/*public static boolean startRemindThreadWithBatteryInfo(Context context, ControlCenterServiceHandler handler, boolean isCharging, int batteryLevel) {
LogUtils.d(TAG, String.format("startRemindThreadWithBatteryInfo调用 | context=%s | handler=%s | isCharging=%b | batteryLevel=%d", context, handler, isCharging, batteryLevel));
// 入参严格校验
@@ -143,13 +143,13 @@ public class RemindThread extends Thread {
RemindThread newRemindThread = new RemindThread(context, handler);
// 同步电池状态(范围校验)
newRemindThread.isCharging = isCharging;
newRemindThread.quantityOfElectricity = Math.min(Math.max(batteryLevel, BATTERY_LEVEL_MIN), BATTERY_LEVEL_MAX);
newRemindThread.sQuantityOfElectricity = Math.min(Math.max(batteryLevel, BATTERY_LEVEL_MIN), BATTERY_LEVEL_MAX);
newRemindThread.isExist = false;
newRemindThread.start();
sRemindThreadList.add(newRemindThread);
LogUtils.d(TAG, String.format("新线程启动成功 | threadId=%d | 电池状态同步完成(电量=%d充电=%b", newRemindThread.getId(), newRemindThread.quantityOfElectricity, newRemindThread.isCharging));
LogUtils.d(TAG, String.format("新线程启动成功 | threadId=%d | 电池状态同步完成(电量=%d充电=%b", newRemindThread.getId(), newRemindThread.sQuantityOfElectricity, newRemindThread.isCharging));
return true;
}
}*/
/**
* 安全停止所有线程,清空列表
@@ -213,21 +213,21 @@ public class RemindThread extends Thread {
if (isExist) break;
// 电量有效性校验非0-100视为无效退出电量提醒线程
if (quantityOfElectricity < BATTERY_LEVEL_MIN || quantityOfElectricity > BATTERY_LEVEL_MAX) {
LogUtils.w(TAG, String.format("电量无效,退出电量提醒线程 | 当前电量=%d | threadId=%d", quantityOfElectricity, getId()));
if (sQuantityOfElectricity < BATTERY_LEVEL_MIN || sQuantityOfElectricity > BATTERY_LEVEL_MAX) {
LogUtils.w(TAG, String.format("电量无效,退出电量提醒线程 | 当前电量=%d | threadId=%d", sQuantityOfElectricity, getId()));
break;
}
// 充电/耗电提醒触发逻辑
boolean chargeRemindTrigger = isCharging && isEnableChargeReminder && quantityOfElectricity >= chargeReminderValue;
boolean usageRemindTrigger = !isCharging && isEnableUsageReminder && quantityOfElectricity <= usageReminderValue;
boolean chargeRemindTrigger = isCharging && isEnableChargeReminder && sQuantityOfElectricity >= chargeReminderValue;
boolean usageRemindTrigger = !isCharging && isEnableUsageReminder && sQuantityOfElectricity <= usageReminderValue;
if (chargeRemindTrigger) {
LogUtils.d(TAG, String.format("触发充电提醒 | 当前电量=%d ≥ 阈值=%d | threadId=%d", quantityOfElectricity, chargeReminderValue, getId()));
sendNotificationMessageInternal(REMIND_TYPE_CHARGE, quantityOfElectricity, isCharging);
LogUtils.d(TAG, String.format("触发充电提醒 | 当前电量=%d ≥ 阈值=%d | threadId=%d", sQuantityOfElectricity, chargeReminderValue, getId()));
sendNotificationMessageInternal(REMIND_TYPE_CHARGE, sQuantityOfElectricity, isCharging);
} else if (usageRemindTrigger) {
LogUtils.d(TAG, String.format("触发耗电提醒 | 当前电量=%d ≤ 阈值=%d | threadId=%d", quantityOfElectricity, usageReminderValue, getId()));
sendNotificationMessageInternal(REMIND_TYPE_USAGE, quantityOfElectricity, isCharging);
LogUtils.d(TAG, String.format("触发耗电提醒 | 当前电量=%d ≤ 阈值=%d | threadId=%d", sQuantityOfElectricity, usageReminderValue, getId()));
sendNotificationMessageInternal(REMIND_TYPE_USAGE, sQuantityOfElectricity, isCharging);
} else {
LogUtils.d(TAG, String.format("未有合适类型提醒,退出提醒线程 | threadId=%d", getId()));
break;
@@ -237,7 +237,7 @@ public class RemindThread extends Thread {
safeSleepInternal(sleepTime);
} catch (Exception e) {
LogUtils.e(TAG, String.format("循环运行异常,退出电量提醒线程 | 当前电量=%d | threadId=%d", quantityOfElectricity, getId()), e);
LogUtils.e(TAG, String.format("循环运行异常,退出电量提醒线程 | 当前电量=%d | threadId=%d", sQuantityOfElectricity, getId()), e);
break;
}
}
@@ -315,7 +315,6 @@ public class RemindThread extends Thread {
sleepTime = MIN_SLEEP_TIME;
chargeReminderValue = -1;
usageReminderValue = -1;
quantityOfElectricity = INVALID_BATTERY_VALUE;
isCharging = false;
LogUtils.d(TAG, String.format("线程初始状态重置完成 | threadId=%d", getId()));
}
@@ -327,7 +326,7 @@ public class RemindThread extends Thread {
LogUtils.d(TAG, String.format("cleanThreadStateInternal调用 | threadId=%d", getId()));
isReminding = false;
isExist = true;
quantityOfElectricity = INVALID_BATTERY_VALUE;
sQuantityOfElectricity = INVALID_BATTERY_VALUE;
// 中断当前线程(如果存活)
if (isAlive()) {
interrupt();
@@ -344,7 +343,7 @@ public class RemindThread extends Thread {
LogUtils.d(TAG, String.format("setAppConfigBean调用 | config=%s | threadId=%d", config, getId()));
if (config == null) {
LogUtils.e(TAG, String.format("配置同步失败配置Bean为空 | threadId=%d", getId()));
quantityOfElectricity = INVALID_BATTERY_VALUE;
sQuantityOfElectricity = INVALID_BATTERY_VALUE;
return;
}
@@ -354,12 +353,12 @@ public class RemindThread extends Thread {
chargeReminderValue = Math.min(Math.max(config.getChargeReminderValue(), BATTERY_LEVEL_MIN), BATTERY_LEVEL_MAX);
usageReminderValue = Math.min(Math.max(config.getUsageReminderValue(), BATTERY_LEVEL_MIN), BATTERY_LEVEL_MAX);
sleepTime = Math.max(config.getBatteryDetectInterval(), MIN_SLEEP_TIME);
quantityOfElectricity = (config.getCurrentBatteryValue() >= BATTERY_LEVEL_MIN && config.getCurrentBatteryValue() <= BATTERY_LEVEL_MAX)
? config.getCurrentBatteryValue() : INVALID_BATTERY_VALUE;
// sQuantityOfElectricity = (config.getCurrentBatteryValue() >= BATTERY_LEVEL_MIN && config.getCurrentBatteryValue() <= BATTERY_LEVEL_MAX)
// ? config.getCurrentBatteryValue() : INVALID_BATTERY_VALUE;
isCharging = config.isCharging();
LogUtils.d(TAG, String.format("配置同步完成 | 休眠时间=%dms | 充电提醒=%b | 耗电提醒=%b | 当前电量=%d | 充电阈值=%d | 耗电阈值=%d | threadId=%d",
sleepTime, isEnableChargeReminder, isEnableUsageReminder, quantityOfElectricity, chargeReminderValue, usageReminderValue, getId()));
sleepTime, isEnableChargeReminder, isEnableUsageReminder, sQuantityOfElectricity, chargeReminderValue, usageReminderValue, getId()));
}
/**
@@ -392,7 +391,7 @@ public class RemindThread extends Thread {
", isReminding=" + isReminding +
", chargeThreshold=" + chargeReminderValue +
", usageThreshold=" + usageReminderValue +
", currentBattery=" + quantityOfElectricity +
", currentBattery=" + sQuantityOfElectricity +
", isCharging=" + isCharging +
", sleepTime=" + sleepTime + "ms" +
'}';

View File

@@ -5,6 +5,7 @@ import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.models.AppConfigBean;
import cc.winboll.studio.powerbell.models.ControlCenterServiceBean;
import cc.winboll.studio.powerbell.threads.RemindThread;
/**
* 应用配置工具类:管理应用核心配置(服务开关、电池提醒阈值、背景设置等)
@@ -79,7 +80,7 @@ public class AppConfigUtils {
public AppConfigBean loadAppConfig() {
LogUtils.d(TAG, "loadAppConfig调用开始加载应用配置");
AppConfigBean savedAppBean = (AppConfigBean) AppConfigBean.loadBean(mContext, AppConfigBean.class);
if (savedAppBean != null) {
mAppConfigBean = savedAppBean;
LogUtils.d(TAG, String.format("loadAppConfig成功 | 充电阈值=%d%% | 耗电阈值=%d%%",
@@ -248,12 +249,12 @@ public class AppConfigUtils {
LogUtils.d(TAG, String.format("setCurrentBatteryValue调用 | 传入电量=%d", value));
int calibratedValue = Math.min(Math.max(value, MIN_REMINDER_VALUE), MAX_REMINDER_VALUE);
if (calibratedValue == mAppConfigBean.getCurrentBatteryValue()) {
if (calibratedValue == RemindThread.sQuantityOfElectricity) {
LogUtils.d(TAG, "setCurrentBatteryValue电池电量无变化无需操作");
return;
}
mAppConfigBean.setCurrentBatteryValue(calibratedValue);
RemindThread.sQuantityOfElectricity = calibratedValue;
LogUtils.d(TAG, String.format("setCurrentBatteryValue成功 | 电池电量=%d%%", calibratedValue));
}
@@ -262,7 +263,7 @@ public class AppConfigUtils {
* @return 当前电池电量0-100
*/
public int getCurrentBatteryValue() {
int value = mAppConfigBean.getCurrentBatteryValue();
int value = RemindThread.sQuantityOfElectricity;
LogUtils.d(TAG, String.format("getCurrentBatteryValue获取电池电量=%d%%", value));
return value;
}