@@ -18,8 +18,7 @@ public class RemindThread extends Thread {
public static final String TAG = " RemindThread " ;
// 时间常量 (ms)
private static final int MIN_SLEEP_TIME = 5 00;
private static final int DEFAULT_SLEEP_TIME = 1000 ;
private static final int MIN_SLEEP_TIME = 20 00;
private static final long THREAD_JOIN_TIMEOUT = 1000L ;
// 状态常量
@@ -53,7 +52,7 @@ public class RemindThread extends Thread {
private volatile boolean isCharging ;
// 并发安全锁(保护线程状态变更)
private final Object m Lock = new Object ( ) ;
private final static Object sRemind Lock = 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 ( m Lock) {
if ( ! isExist & & ! isReminding ) {
isReminding = true ;
LogUtils . d ( TAG , " 提醒状态初始化成功 | isReminding=true " ) ;
} else {
LogUtils . d ( TAG , " 线程退出条件满足 | isExist= " + isExist + " | isReminding= " + isReminding ) ;
cleanThreadStateInternal ( ) ;
synchronized ( sRemind Lock) {
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 ;