调整电量消息频率以及消息内容。

This commit is contained in:
2025-12-20 16:30:34 +08:00
parent 6af2096b30
commit 65acbfcd04
4 changed files with 68 additions and 70 deletions

View File

@@ -29,8 +29,8 @@ public class ControlCenterServiceHandler extends Handler {
// 通知文案常量(抽离魔法值,便于统一修改)
private static final String CHARGE_REMIND_TITLE = "充电提醒";
private static final String USAGE_REMIND_TITLE = "耗电提醒";
private static final String CHARGE_REMIND_CONTENT_FORMAT = "(+) 当前电量%d%%%s,已达标建议及时断电,保护电池寿命~";
private static final String USAGE_REMIND_CONTENT_FORMAT = "(-) 当前电量%d%%%s,已偏低建议及时充电,避免设备关机~";
private static final String CHARGE_REMIND_CONTENT_FORMAT = "(+)电量已达额定值。当前电量%d%%%s";
private static final String USAGE_REMIND_CONTENT_FORMAT = "(-)电量低于指定值。当前电量%d%%%s";
private static final String CHARGE_STATE_CHARGING = "充电中";
private static final String CHARGE_STATE_NOT_CHARGING = "未充电";

View File

@@ -29,7 +29,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
int reminderIntervalTime = 5000; // 铃声提醒间隔ms原有
boolean isCharging = false; // 是否充电(状态字段,原有)
int currentBatteryValue = -1; // 修正统一命名为「currentBatteryValue」原 currentValue
int batteryDetectInterval = 1000; // 新增电量检测间隔ms适配 RemindThread
int batteryDetectInterval = 2000; // 新增电量检测间隔ms适配 RemindThread
// 构造方法:初始化默认配置(同步修正字段名,统一默认值)
public AppConfigBean() {

View File

@@ -18,8 +18,7 @@ public class RemindThread extends Thread {
public static final String TAG = "RemindThread";
// 时间常量 (ms)
private static final int MIN_SLEEP_TIME = 500;
private static final int DEFAULT_SLEEP_TIME = 1000;
private static final int MIN_SLEEP_TIME = 2000;
private static final long THREAD_JOIN_TIMEOUT = 1000L;
// 状态常量
@@ -53,7 +52,7 @@ public class RemindThread extends Thread {
private volatile boolean isCharging;
// 并发安全锁(保护线程状态变更)
private final Object mLock = new Object();
private final static Object sRemindLock = new Object();
// ================================== 私有构造器(单例专用,禁止外部实例化)=================================
private RemindThread(Context context, ControlCenterServiceHandler handler) {
@@ -93,31 +92,33 @@ public class RemindThread extends Thread {
*/
public static boolean startRemindThreadWithAppConfig(Context context, ControlCenterServiceHandler handler, AppConfigBean config) {
LogUtils.d(TAG, "startRemindThreadWithAppConfig执行 | context=" + context + " | handler=" + handler + " | config=" + config);
// 入参严格校验
if (context == null || handler == null || config == null) {
LogUtils.e(TAG, "启动失败:入参为空 | context=" + context + " | handler=" + handler + " | config=" + config);
return false;
}
synchronized (RemindThread.class) {
// 入参严格校验
if (context == null || handler == null || config == null) {
LogUtils.e(TAG, "启动失败:入参为空 | context=" + context + " | handler=" + handler + " | config=" + config);
return false;
}
RemindThread instance = getInstance(context, handler);
// 已在提醒状态,仅同步配置
if (instance.isReminding) {
instance.setAppConfigBean(config);
LogUtils.d(TAG, "线程已在运行,同步最新配置 | threadId=" + instance.getId());
return true;
}
RemindThread instance = getInstance(context, handler);
// 已在提醒状态,仅同步配置
if (instance.isReminding) {
instance.setAppConfigBean(config);
LogUtils.d(TAG, "线程已在运行,同步最新配置 | threadId=" + instance.getId());
return true;
}
// 同步配置并启动线程
instance.setAppConfigBean(config);
if (!instance.isRunning()) {
instance.isExist = false;
instance.start();
LogUtils.d(TAG, "线程启动成功 | threadId=" + instance.getId());
return true;
} else {
LogUtils.d(TAG, "线程已在运行状态 | threadId=" + instance.getId());
return true;
}
// 同步配置并启动线程
instance.setAppConfigBean(config);
if (!instance.isRunning()) {
instance.isExist = false;
instance.start();
LogUtils.d(TAG, "线程启动成功 | threadId=" + instance.getId());
return true;
} else {
LogUtils.d(TAG, "线程已在运行状态 | threadId=" + instance.getId());
return true;
}
}
}
/**
@@ -130,33 +131,35 @@ public class RemindThread extends Thread {
*/
public static boolean startRemindThreadWithBatteryInfo(Context context, ControlCenterServiceHandler handler, boolean isCharging, int lastBatteryLevel) {
LogUtils.d(TAG, "startRemindThreadWithBatteryInfo执行 | context=" + context + " | handler=" + handler + " | isCharging=" + isCharging + " | lastBatteryLevel=" + lastBatteryLevel);
// 入参严格校验
if (context == null || handler == null) {
LogUtils.e(TAG, "启动失败:入参为空 | context=" + context + " | handler=" + handler);
return false;
}
synchronized (RemindThread.class) {
// 入参严格校验
if (context == null || handler == null) {
LogUtils.e(TAG, "启动失败:入参为空 | context=" + context + " | handler=" + handler);
return false;
}
RemindThread instance = getInstance(context, handler);
// 已在提醒状态,仅同步电池信息
if (instance.isReminding) {
instance.isCharging = isCharging;
instance.quantityOfElectricity = lastBatteryLevel;
LogUtils.d(TAG, "线程已在运行,同步电池信息 | threadId=" + instance.getId() + " | isCharging=" + isCharging + " | lastBatteryLevel=" + lastBatteryLevel);
return true;
}
RemindThread instance = getInstance(context, handler);
// 已在提醒状态,仅同步电池信息
if (instance.isReminding) {
instance.isCharging = isCharging;
instance.quantityOfElectricity = lastBatteryLevel;
LogUtils.d(TAG, "线程已在运行,同步电池信息 | threadId=" + instance.getId() + " | isCharging=" + isCharging + " | lastBatteryLevel=" + lastBatteryLevel);
return true;
}
// 同步电池信息并启动线程
instance.isCharging = isCharging;
instance.quantityOfElectricity = lastBatteryLevel;
if (!instance.isRunning()) {
instance.isExist = false;
instance.start();
LogUtils.d(TAG, "线程启动成功 | threadId=" + instance.getId());
return true;
} else {
LogUtils.d(TAG, "线程已在运行状态 | threadId=" + instance.getId());
return true;
}
// 同步电池信息并启动线程
instance.isCharging = isCharging;
instance.quantityOfElectricity = lastBatteryLevel;
if (!instance.isRunning()) {
instance.isExist = false;
instance.start();
LogUtils.d(TAG, "线程启动成功 | threadId=" + instance.getId());
return true;
} else {
LogUtils.d(TAG, "线程已在运行状态 | threadId=" + instance.getId());
return true;
}
}
}
/**
@@ -223,15 +226,11 @@ public class RemindThread extends Thread {
LogUtils.d(TAG, "run执行 | threadId=" + getId() + " | 状态=" + getState());
// 初始化提醒状态(加锁保护,避免多线程竞争)
synchronized (mLock) {
if (!isExist && !isReminding) {
isReminding = true;
LogUtils.d(TAG, "提醒状态初始化成功 | isReminding=true");
} else {
LogUtils.d(TAG, "线程退出条件满足 | isExist=" + isExist + " | isReminding=" + isReminding);
cleanThreadStateInternal();
synchronized (sRemindLock) {
if (isReminding) {
return;
}
isReminding = true;
}
// 核心电量检测循环
@@ -241,11 +240,10 @@ public class RemindThread extends Thread {
// 快速退出判断
if (isExist) break;
// 电量有效性校验非0-100视为无效
// 电量有效性校验非0-100视为无效,退出电量提醒线程
if (quantityOfElectricity < BATTERY_LEVEL_MIN || quantityOfElectricity > BATTERY_LEVEL_MAX) {
LogUtils.w(TAG, "电量无效,跳过本次检测 | 当前电量=" + quantityOfElectricity);
safeSleepInternal(sleepTime);
continue;
LogUtils.w(TAG, "电量无效,退出电量提醒线程 | 当前电量=" + quantityOfElectricity);
break;
}
// 充电提醒触发逻辑
@@ -260,8 +258,8 @@ public class RemindThread extends Thread {
// 安全休眠,保留中断标记
safeSleepInternal(sleepTime);
} catch (Exception e) {
LogUtils.e(TAG, "循环运行异常,休眠重试", e);
safeSleepInternal(sleepTime);
LogUtils.e(TAG, "循环运行异常,退出电量提醒线程 | 当前电量=" + quantityOfElectricity);
break;
}
}
@@ -333,7 +331,7 @@ public class RemindThread extends Thread {
// 配置参数初始化
isEnableChargeReminder = false;
isEnableUsageReminder = false;
sleepTime = DEFAULT_SLEEP_TIME;
sleepTime = MIN_SLEEP_TIME;
chargeReminderValue = -1;
usageReminderValue = -1;
quantityOfElectricity = INVALID_BATTERY_VALUE;