移除提醒线程前台标志
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Wed Dec 17 12:37:54 GMT 2025
|
#Thu Dec 18 05:08:37 GMT 2025
|
||||||
stageCount=10
|
stageCount=10
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.14
|
baseVersion=15.14
|
||||||
publishVersion=15.14.9
|
publishVersion=15.14.9
|
||||||
buildCount=25
|
buildCount=26
|
||||||
baseBetaVersion=15.14.10
|
baseBetaVersion=15.14.10
|
||||||
|
|||||||
@@ -132,8 +132,8 @@ public class MainActivity extends WinBoLLActivity implements MainContentView.OnV
|
|||||||
if (mADsBannerView != null) {
|
if (mADsBannerView != null) {
|
||||||
mADsBannerView.resumeADs(this);
|
mADsBannerView.resumeADs(this);
|
||||||
}
|
}
|
||||||
// 设置RemindThread前台状态
|
// // 设置RemindThread前台状态
|
||||||
setRemindThreadForeground(true);
|
// setRemindThreadForeground(true);
|
||||||
LogUtils.d(TAG, "onResume: 退出");
|
LogUtils.d(TAG, "onResume: 退出");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,8 +141,8 @@ public class MainActivity extends WinBoLLActivity implements MainContentView.OnV
|
|||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
LogUtils.d(TAG, "onPause: 进入");
|
LogUtils.d(TAG, "onPause: 进入");
|
||||||
super.onPause();
|
super.onPause();
|
||||||
// 设置RemindThread后台状态
|
// // 设置RemindThread后台状态
|
||||||
setRemindThreadForeground(false);
|
// setRemindThreadForeground(false);
|
||||||
LogUtils.d(TAG, "onPause: 退出");
|
LogUtils.d(TAG, "onPause: 退出");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -521,19 +521,19 @@ public class MainActivity extends WinBoLLActivity implements MainContentView.OnV
|
|||||||
/**
|
/**
|
||||||
* 设置线程前后台状态(依赖服务运行状态)
|
* 设置线程前后台状态(依赖服务运行状态)
|
||||||
*/
|
*/
|
||||||
private void setRemindThreadForeground(boolean isForeground) {
|
// private void setRemindThreadForeground(boolean isForeground) {
|
||||||
LogUtils.d(TAG, "setRemindThreadForeground: 进入,前台状态=" + isForeground);
|
// LogUtils.d(TAG, "setRemindThreadForeground: 进入,前台状态=" + isForeground);
|
||||||
if (!ServiceUtils.isServiceAlive(getApplicationContext(), ControlCenterService.class.getName())) {
|
// if (!ServiceUtils.isServiceAlive(getApplicationContext(), ControlCenterService.class.getName())) {
|
||||||
LogUtils.w(TAG, "setRemindThreadForeground: 服务未运行,跳过状态设置");
|
// LogUtils.w(TAG, "setRemindThreadForeground: 服务未运行,跳过状态设置");
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
try {
|
// try {
|
||||||
RemindThread.getInstance(getApplicationContext(), null).setAppForeground(isForeground);
|
// RemindThread.getInstance(getApplicationContext(), null).setAppForeground(isForeground);
|
||||||
} catch (IllegalArgumentException e) {
|
// } catch (IllegalArgumentException e) {
|
||||||
LogUtils.w(TAG, "setRemindThreadForeground: 线程未初始化", e);
|
// LogUtils.w(TAG, "setRemindThreadForeground: 线程未初始化", e);
|
||||||
}
|
// }
|
||||||
LogUtils.d(TAG, "setRemindThreadForeground: 退出");
|
// LogUtils.d(TAG, "setRemindThreadForeground: 退出");
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 切换服务启用状态(仅持久化本地配置,点击UI开关时不联动服务启停)
|
* 切换服务启用状态(仅持久化本地配置,点击UI开关时不联动服务启停)
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ public class ControlCenterService extends Service {
|
|||||||
public static final String ACTION_RESTART_REMIND_THREAD = "cc.winboll.studio.powerbell.action.RESTART_REMIND_THREAD";
|
public static final String ACTION_RESTART_REMIND_THREAD = "cc.winboll.studio.powerbell.action.RESTART_REMIND_THREAD";
|
||||||
public static final String EXTRA_APP_CONFIG_BEAN = "cc.winboll.studio.powerbell.extra.APP_CONFIG_BEAN";
|
public static final String EXTRA_APP_CONFIG_BEAN = "cc.winboll.studio.powerbell.extra.APP_CONFIG_BEAN";
|
||||||
// 新增:应用前台/后台状态同步指令(用于MainActivity与服务联动)
|
// 新增:应用前台/后台状态同步指令(用于MainActivity与服务联动)
|
||||||
public static final String ACTION_SYNC_APP_FOREGROUND_STATE = "cc.winboll.studio.powerbell.action.SYNC_APP_FOREGROUND_STATE";
|
// public static final String ACTION_SYNC_APP_FOREGROUND_STATE = "cc.winboll.studio.powerbell.action.SYNC_APP_FOREGROUND_STATE";
|
||||||
public static final String EXTRA_APP_FOREGROUND_STATE = "cc.winboll.studio.powerbell.extra.APP_FOREGROUND_STATE";
|
// public static final String EXTRA_APP_FOREGROUND_STATE = "cc.winboll.studio.powerbell.extra.APP_FOREGROUND_STATE";
|
||||||
|
|
||||||
// ================================== 核心成员变量(按依赖优先级排序,私有封装)=================================
|
// ================================== 核心成员变量(按依赖优先级排序,私有封装)=================================
|
||||||
// 服务控制核心(本地持久化管理,替代Intent传递)
|
// 服务控制核心(本地持久化管理,替代Intent传递)
|
||||||
@@ -93,8 +93,8 @@ public class ControlCenterService extends Service {
|
|||||||
handleExternalCommand(intent);
|
handleExternalCommand(intent);
|
||||||
// ================================== 【修复点2:删除rebindForegroundNotify(),避免重复调用】=================================
|
// ================================== 【修复点2:删除rebindForegroundNotify(),避免重复调用】=================================
|
||||||
initServiceBusinessIfNeed(); // 按需初始化业务(避免重复初始化)
|
initServiceBusinessIfNeed(); // 按需初始化业务(避免重复初始化)
|
||||||
// 新增:业务初始化完成后,同步应用前台状态到线程(防止状态丢失)
|
// // 新增:业务初始化完成后,同步应用前台状态到线程(防止状态丢失)
|
||||||
syncAppForegroundStateToThread();
|
// syncAppForegroundStateToThread();
|
||||||
LogUtils.d(TAG, "onStartCommand: 核心逻辑执行完成,返回START_STICKY");
|
LogUtils.d(TAG, "onStartCommand: 核心逻辑执行完成,返回START_STICKY");
|
||||||
return START_STICKY;
|
return START_STICKY;
|
||||||
} else {
|
} else {
|
||||||
@@ -373,8 +373,8 @@ public class ControlCenterService extends Service {
|
|||||||
mRemindThread = RemindThread.getInstance(this, mServiceHandler);
|
mRemindThread = RemindThread.getInstance(this, mServiceHandler);
|
||||||
// 同步配置并开启提醒
|
// 同步配置并开启提醒
|
||||||
syncConfigToRemindThread();
|
syncConfigToRemindThread();
|
||||||
// 新增:同步应用前台状态(确保新线程启动时状态正确)
|
// // 新增:同步应用前台状态(确保新线程启动时状态正确)
|
||||||
syncAppForegroundStateToThread();
|
// syncAppForegroundStateToThread();
|
||||||
|
|
||||||
// 双重校验,避免线程重复启动
|
// 双重校验,避免线程重复启动
|
||||||
if (mRemindThread != null && !mRemindThread.isAlive() && !mRemindThread.isThreadStarted()) {
|
if (mRemindThread != null && !mRemindThread.isAlive() && !mRemindThread.isThreadStarted()) {
|
||||||
@@ -454,18 +454,18 @@ public class ControlCenterService extends Service {
|
|||||||
* 新增:同步应用前台状态到RemindThread(核心联动逻辑)
|
* 新增:同步应用前台状态到RemindThread(核心联动逻辑)
|
||||||
* 确保线程实时感知应用前台/后台状态,控制提醒开关
|
* 确保线程实时感知应用前台/后台状态,控制提醒开关
|
||||||
*/
|
*/
|
||||||
private void syncAppForegroundStateToThread() {
|
// private void syncAppForegroundStateToThread() {
|
||||||
LogUtils.d(TAG, "syncAppForegroundStateToThread: 同步前台状态,当前状态=" + mIsAppForeground);
|
// LogUtils.d(TAG, "syncAppForegroundStateToThread: 同步前台状态,当前状态=" + mIsAppForeground);
|
||||||
synchronized (mServiceLock) {
|
// synchronized (mServiceLock) {
|
||||||
if (mRemindThread == null || mIsDestroyed || !isServiceRunning) {
|
// if (mRemindThread == null || mIsDestroyed || !isServiceRunning) {
|
||||||
LogUtils.w(TAG, "syncAppForegroundStateToThread: 线程未启动/服务已销毁,同步失败");
|
// LogUtils.w(TAG, "syncAppForegroundStateToThread: 线程未启动/服务已销毁,同步失败");
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
// 调用RemindThread新增方法,同步前台状态(线程安全)
|
// // 调用RemindThread新增方法,同步前台状态(线程安全)
|
||||||
mRemindThread.setAppForeground(mIsAppForeground);
|
// mRemindThread.setAppForeground(mIsAppForeground);
|
||||||
LogUtils.d(TAG, "syncAppForegroundStateToThread: 前台状态同步完成");
|
// LogUtils.d(TAG, "syncAppForegroundStateToThread: 前台状态同步完成");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// ================================== 外部指令处理方法(新增前台状态同步指令,完善链路)=================================
|
// ================================== 外部指令处理方法(新增前台状态同步指令,完善链路)=================================
|
||||||
/**
|
/**
|
||||||
@@ -503,15 +503,16 @@ public class ControlCenterService extends Service {
|
|||||||
LogUtils.w(TAG, "handleExternalCommand: 服务已禁用,忽略线程重启指令");
|
LogUtils.w(TAG, "handleExternalCommand: 服务已禁用,忽略线程重启指令");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (ACTION_SYNC_APP_FOREGROUND_STATE.equals(action)) {
|
|
||||||
boolean isForeground = intent.getBooleanExtra(EXTRA_APP_FOREGROUND_STATE, false);
|
|
||||||
synchronized (mServiceLock) {
|
|
||||||
mIsAppForeground = isForeground;
|
|
||||||
LogUtils.d(TAG, "handleExternalCommand: 收到前台状态同步指令,更新状态=" + isForeground);
|
|
||||||
// 同步状态到线程(实时生效)
|
|
||||||
syncAppForegroundStateToThread();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// else if (ACTION_SYNC_APP_FOREGROUND_STATE.equals(action)) {
|
||||||
|
// boolean isForeground = intent.getBooleanExtra(EXTRA_APP_FOREGROUND_STATE, false);
|
||||||
|
// synchronized (mServiceLock) {
|
||||||
|
// mIsAppForeground = isForeground;
|
||||||
|
// LogUtils.d(TAG, "handleExternalCommand: 收到前台状态同步指令,更新状态=" + isForeground);
|
||||||
|
// // 同步状态到线程(实时生效)
|
||||||
|
// syncAppForegroundStateToThread();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
LogUtils.d(TAG, "handleExternalCommand: 外部指令处理完成");
|
LogUtils.d(TAG, "handleExternalCommand: 外部指令处理完成");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -634,34 +635,34 @@ public class ControlCenterService extends Service {
|
|||||||
* @param context 上下文
|
* @param context 上下文
|
||||||
* @param isForeground true=前台,false=后台
|
* @param isForeground true=前台,false=后台
|
||||||
*/
|
*/
|
||||||
public static void syncAppForegroundState(Context context, boolean isForeground) {
|
// public static void syncAppForegroundState(Context context, boolean isForeground) {
|
||||||
LogUtils.d(TAG, "syncAppForegroundState: 发送前台状态同步指令,状态=" + isForeground);
|
// LogUtils.d(TAG, "syncAppForegroundState: 发送前台状态同步指令,状态=" + isForeground);
|
||||||
if (context == null) {
|
// if (context == null) {
|
||||||
LogUtils.e(TAG, "syncAppForegroundState: Context为空,同步失败");
|
// LogUtils.e(TAG, "syncAppForegroundState: Context为空,同步失败");
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
if (!isServiceRunning(context, ControlCenterService.class)) {
|
// if (!isServiceRunning(context, ControlCenterService.class)) {
|
||||||
LogUtils.w(TAG, "syncAppForegroundState: 服务未运行,跳过同步");
|
// LogUtils.w(TAG, "syncAppForegroundState: 服务未运行,跳过同步");
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
Intent intent = new Intent(context, ControlCenterService.class);
|
// Intent intent = new Intent(context, ControlCenterService.class);
|
||||||
intent.setAction(ACTION_SYNC_APP_FOREGROUND_STATE);
|
// intent.setAction(ACTION_SYNC_APP_FOREGROUND_STATE);
|
||||||
intent.putExtra(EXTRA_APP_FOREGROUND_STATE, isForeground);
|
// intent.putExtra(EXTRA_APP_FOREGROUND_STATE, isForeground);
|
||||||
intent.setPackage(context.getPackageName());
|
// intent.setPackage(context.getPackageName());
|
||||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
// intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
||||||
|
//
|
||||||
try {
|
// try {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
context.startForegroundService(intent);
|
// context.startForegroundService(intent);
|
||||||
} else {
|
// } else {
|
||||||
context.startService(intent);
|
// context.startService(intent);
|
||||||
}
|
// }
|
||||||
LogUtils.d(TAG, "syncAppForegroundState: 前台状态同步指令发送成功");
|
// LogUtils.d(TAG, "syncAppForegroundState: 前台状态同步指令发送成功");
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
LogUtils.e(TAG, "syncAppForegroundState: 发送同步指令异常", e);
|
// LogUtils.e(TAG, "syncAppForegroundState: 发送同步指令异常", e);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断服务是否运行(适配API30+ ActivityManager限制,优化判断逻辑)
|
* 判断服务是否运行(适配API30+ ActivityManager限制,优化判断逻辑)
|
||||||
@@ -865,12 +866,12 @@ public class ControlCenterService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 新增:设置应用前台状态(内部/外部调用,同步到线程)
|
// 新增:设置应用前台状态(内部/外部调用,同步到线程)
|
||||||
public void setAppForeground(boolean isForeground) {
|
// public void setAppForeground(boolean isForeground) {
|
||||||
LogUtils.d(TAG, "setAppForeground: 更新应用前台状态=" + isForeground);
|
// LogUtils.d(TAG, "setAppForeground: 更新应用前台状态=" + isForeground);
|
||||||
synchronized (mServiceLock) {
|
// synchronized (mServiceLock) {
|
||||||
mIsAppForeground = isForeground;
|
// mIsAppForeground = isForeground;
|
||||||
syncAppForegroundStateToThread();
|
// syncAppForegroundStateToThread();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ public class RemindThread extends Thread {
|
|||||||
private static final int DEFAULT_SLEEP_TIME = 1000; // 默认检测间隔(ms)
|
private static final int DEFAULT_SLEEP_TIME = 1000; // 默认检测间隔(ms)
|
||||||
private static final long REMIND_INTERVAL = 3000L; // 重复提醒间隔(ms,防骚扰)
|
private static final long REMIND_INTERVAL = 3000L; // 重复提醒间隔(ms,防骚扰)
|
||||||
private static final int CONFIG_RETRY_MAX = 3; // 配置同步重试次数(兜底)
|
private static final int CONFIG_RETRY_MAX = 3; // 配置同步重试次数(兜底)
|
||||||
private static final int INVALID_BATTERY_VALUE = -1; // 无效电量标记(新增)
|
private static final int INVALID_BATTERY_VALUE = -1; // 无效电量标记
|
||||||
private static final long BACKGROUND_SLEEP_TIME = 5000L;// 后台休眠间隔(ms,减少资源占用,新增)
|
private static final long BACKGROUND_SLEEP_TIME = 5000L;// 后台休眠间隔(ms,减少资源占用)
|
||||||
|
|
||||||
// ================================== 单例核心(线程安全,避免复用旧实例)=================================
|
// ================================== 单例核心(线程安全,避免复用旧实例)=================================
|
||||||
private static volatile RemindThread sInstance;
|
private static volatile RemindThread sInstance;
|
||||||
@@ -36,7 +36,6 @@ public class RemindThread extends Thread {
|
|||||||
private volatile boolean isThreadStarted; // 启动标记(区分isAlive())
|
private volatile boolean isThreadStarted; // 启动标记(区分isAlive())
|
||||||
private volatile boolean isReminding; // 全局提醒开关
|
private volatile boolean isReminding; // 全局提醒开关
|
||||||
private volatile boolean isRemindTimerRunning; // 提醒恢复计时器状态
|
private volatile boolean isRemindTimerRunning; // 提醒恢复计时器状态
|
||||||
private volatile boolean isAppForeground; // 应用前台状态(新增:控制提醒开关)
|
|
||||||
|
|
||||||
// 业务配置(实时同步,范围校验)
|
// 业务配置(实时同步,范围校验)
|
||||||
private volatile boolean isEnableChargeReminder; // 充电提醒开关
|
private volatile boolean isEnableChargeReminder; // 充电提醒开关
|
||||||
@@ -105,7 +104,7 @@ public class RemindThread extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================================== 线程核心逻辑(run方法,高效退出+异常容错+前台判断)=================================
|
// ================================== 线程核心逻辑(run方法,高效退出+异常容错)=================================
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
LogUtils.d(TAG, "线程启动,ID:" + Thread.currentThread().getId() + ",状态:" + getState());
|
LogUtils.d(TAG, "线程启动,ID:" + Thread.currentThread().getId() + ",状态:" + getState());
|
||||||
@@ -117,7 +116,6 @@ public class RemindThread extends Thread {
|
|||||||
}
|
}
|
||||||
isThreadStarted = true;
|
isThreadStarted = true;
|
||||||
lastRemindTime = 0;
|
lastRemindTime = 0;
|
||||||
isAppForeground = true; // 初始默认前台(窗口打开状态)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 配置同步重试(失败则兜底默认值)
|
// 配置同步重试(失败则兜底默认值)
|
||||||
@@ -142,19 +140,14 @@ public class RemindThread extends Thread {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 核心检测循环(快速退出+并发安全+前台判断)
|
// 核心检测循环(快速退出+并发安全)
|
||||||
LogUtils.d(TAG, "进入电量检测循环,间隔:" + sleepTime + "ms");
|
LogUtils.d(TAG, "进入电量检测循环,间隔:" + sleepTime + "ms");
|
||||||
while (!isExist) {
|
while (!isExist) {
|
||||||
try {
|
try {
|
||||||
if (isExist) break;
|
if (isExist) break;
|
||||||
|
|
||||||
// 核心防护:应用后台/提醒关闭/电量无效,直接休眠(跳过提醒检测)
|
// 核心防护:提醒关闭/电量无效,直接休眠(跳过提醒检测)
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
// 应用后台:延长休眠间隔,减少资源占用+停止提醒
|
|
||||||
if (!isAppForeground) {
|
|
||||||
safeSleep(BACKGROUND_SLEEP_TIME);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// 提醒关闭/电量无效:正常休眠,不检测
|
// 提醒关闭/电量无效:正常休眠,不检测
|
||||||
if (!isReminding || quantityOfElectricity == INVALID_BATTERY_VALUE) {
|
if (!isReminding || quantityOfElectricity == INVALID_BATTERY_VALUE) {
|
||||||
safeSleep(sleepTime);
|
safeSleep(sleepTime);
|
||||||
@@ -169,9 +162,9 @@ public class RemindThread extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 锁保护核心提醒逻辑(仅前台+提醒开启+电量有效时执行)
|
// 锁保护核心提醒逻辑(仅提醒开启+电量有效时执行)
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
if (isExist || !isAppForeground || !isReminding || quantityOfElectricity == INVALID_BATTERY_VALUE) {
|
if (isExist || !isReminding || quantityOfElectricity == INVALID_BATTERY_VALUE) {
|
||||||
safeSleep(sleepTime);
|
safeSleep(sleepTime);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -253,9 +246,9 @@ public class RemindThread extends Thread {
|
|||||||
*/
|
*/
|
||||||
private void sendNotificationMessage(String type, int battery, boolean isCharging) {
|
private void sendNotificationMessage(String type, int battery, boolean isCharging) {
|
||||||
LogUtils.d(TAG, "准备发送提醒消息:类型=" + type + ",电量=" + battery + ",充电状态=" + isCharging);
|
LogUtils.d(TAG, "准备发送提醒消息:类型=" + type + ",电量=" + battery + ",充电状态=" + isCharging);
|
||||||
// 双重防护:线程退出/应用后台/提醒关闭,跳过发送
|
// 双重防护:线程退出/提醒关闭,跳过发送
|
||||||
if (isExist || !isAppForeground || !isReminding) {
|
if (isExist || !isReminding) {
|
||||||
LogUtils.d(TAG, "线程退出/应用后台/提醒关闭,跳过发送");
|
LogUtils.d(TAG, "线程退出/提醒关闭,跳过发送");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +278,7 @@ public class RemindThread extends Thread {
|
|||||||
private void startRemindRecoveryTimer() {
|
private void startRemindRecoveryTimer() {
|
||||||
LogUtils.d(TAG, "启动提醒恢复计时器,间隔:" + REMIND_INTERVAL + "ms");
|
LogUtils.d(TAG, "启动提醒恢复计时器,间隔:" + REMIND_INTERVAL + "ms");
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
if (isExist || isRemindTimerRunning || sInstance != this || !isAppForeground) {
|
if (isExist || isRemindTimerRunning || sInstance != this) {
|
||||||
LogUtils.w(TAG, "计时器启动条件不满足,跳过");
|
LogUtils.w(TAG, "计时器启动条件不满足,跳过");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -306,8 +299,8 @@ public class RemindThread extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
synchronized (thread.mLock) {
|
synchronized (thread.mLock) {
|
||||||
// 仅前台+有启用提醒时,才恢复提醒
|
// 仅线程存活+有启用提醒时,才恢复提醒
|
||||||
if (!thread.isExist && sInstance == thread && thread.isAppForeground
|
if (!thread.isExist && sInstance == thread
|
||||||
&& (thread.isEnableChargeReminder || thread.isEnableUsageReminder)) {
|
&& (thread.isEnableChargeReminder || thread.isEnableUsageReminder)) {
|
||||||
thread.isReminding = true;
|
thread.isReminding = true;
|
||||||
LogUtils.d(TAG, "提醒功能恢复开启");
|
LogUtils.d(TAG, "提醒功能恢复开启");
|
||||||
@@ -339,7 +332,6 @@ public class RemindThread extends Thread {
|
|||||||
isExist = true;
|
isExist = true;
|
||||||
isReminding = false;
|
isReminding = false;
|
||||||
isRemindTimerRunning = false;
|
isRemindTimerRunning = false;
|
||||||
isAppForeground = false; // 标记后台,强制停止提醒
|
|
||||||
}
|
}
|
||||||
if (isAlive()) {
|
if (isAlive()) {
|
||||||
interrupt();
|
interrupt();
|
||||||
@@ -379,7 +371,6 @@ public class RemindThread extends Thread {
|
|||||||
isExist = true;
|
isExist = true;
|
||||||
isThreadStarted = false;
|
isThreadStarted = false;
|
||||||
isRemindTimerRunning = false;
|
isRemindTimerRunning = false;
|
||||||
isAppForeground = false;
|
|
||||||
lastRemindTime = 0;
|
lastRemindTime = 0;
|
||||||
quantityOfElectricity = INVALID_BATTERY_VALUE; // 重置为无效电量,防残留
|
quantityOfElectricity = INVALID_BATTERY_VALUE; // 重置为无效电量,防残留
|
||||||
}
|
}
|
||||||
@@ -399,7 +390,6 @@ public class RemindThread extends Thread {
|
|||||||
isReminding = false;
|
isReminding = false;
|
||||||
isThreadStarted = false;
|
isThreadStarted = false;
|
||||||
isRemindTimerRunning = false;
|
isRemindTimerRunning = false;
|
||||||
isAppForeground = false;
|
|
||||||
lastRemindTime = 0;
|
lastRemindTime = 0;
|
||||||
// 业务配置
|
// 业务配置
|
||||||
isEnableChargeReminder = false;
|
isEnableChargeReminder = false;
|
||||||
@@ -421,7 +411,7 @@ public class RemindThread extends Thread {
|
|||||||
isExist = false;
|
isExist = false;
|
||||||
isRemindTimerRunning = false;
|
isRemindTimerRunning = false;
|
||||||
lastRemindTime = 0;
|
lastRemindTime = 0;
|
||||||
isReminding = (isEnableChargeReminder || isEnableUsageReminder) && isAppForeground; // 仅前台时开启提醒
|
isReminding = (isEnableChargeReminder || isEnableUsageReminder); // 根据开关自动开启提醒
|
||||||
}
|
}
|
||||||
LogUtils.d(TAG, "运行状态重置完成,全局提醒:" + isReminding);
|
LogUtils.d(TAG, "运行状态重置完成,全局提醒:" + isReminding);
|
||||||
}
|
}
|
||||||
@@ -452,13 +442,13 @@ public class RemindThread extends Thread {
|
|||||||
int currentBattery = config.getCurrentBatteryValue();
|
int currentBattery = config.getCurrentBatteryValue();
|
||||||
quantityOfElectricity = (currentBattery >= 0 && currentBattery <= 100) ? currentBattery : INVALID_BATTERY_VALUE;
|
quantityOfElectricity = (currentBattery >= 0 && currentBattery <= 100) ? currentBattery : INVALID_BATTERY_VALUE;
|
||||||
isCharging = config.isCharging();
|
isCharging = config.isCharging();
|
||||||
// 提醒开关:前台+有启用提醒才开启
|
// 提醒开关:根据配置自动开启
|
||||||
isReminding = (isEnableChargeReminder || isEnableUsageReminder) && isAppForeground;
|
isReminding = (isEnableChargeReminder || isEnableUsageReminder);
|
||||||
}
|
}
|
||||||
LogUtils.d(TAG, "配置同步完成:检测间隔" + sleepTime + "ms,全局提醒" + isReminding + ",当前电量" + quantityOfElectricity);
|
LogUtils.d(TAG, "配置同步完成:检测间隔" + sleepTime + "ms,全局提醒" + isReminding + ",当前电量" + quantityOfElectricity);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================================== Setter/Getter(锁保护,线程安全+精准日志,新增前台状态控制)=================================
|
// ================================== Setter/Getter(锁保护,线程安全+精准日志)=================================
|
||||||
public boolean isExist() {
|
public boolean isExist() {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
return isExist;
|
return isExist;
|
||||||
@@ -480,32 +470,8 @@ public class RemindThread extends Thread {
|
|||||||
|
|
||||||
public void setIsReminding(boolean isReminding) {
|
public void setIsReminding(boolean isReminding) {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
// 仅前台时可开启提醒
|
this.isReminding = isReminding;
|
||||||
this.isReminding = isReminding && isAppForeground;
|
LogUtils.d(TAG, "设置全局提醒开关:" + this.isReminding);
|
||||||
LogUtils.d(TAG, "设置全局提醒开关:" + this.isReminding + "(应用前台状态:" + isAppForeground + ")");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 新增:应用前台/后台状态设置(主窗口关闭/打开时调用)
|
|
||||||
public void setAppForeground(boolean isForeground) {
|
|
||||||
synchronized (mLock) {
|
|
||||||
this.isAppForeground = isForeground;
|
|
||||||
// 后台时强制关闭提醒,前台时根据开关自动开启
|
|
||||||
if (!isForeground) {
|
|
||||||
this.isReminding = false;
|
|
||||||
this.quantityOfElectricity = INVALID_BATTERY_VALUE; // 后台重置为无效电量
|
|
||||||
LogUtils.d(TAG, "应用切换到后台,停止提醒+重置无效电量");
|
|
||||||
} else {
|
|
||||||
this.isReminding = (isEnableChargeReminder || isEnableUsageReminder);
|
|
||||||
LogUtils.d(TAG, "应用切换到前台,提醒状态:" + this.isReminding);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 新增:获取应用前台状态
|
|
||||||
public boolean isAppForeground() {
|
|
||||||
synchronized (mLock) {
|
|
||||||
return isAppForeground;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -524,7 +490,7 @@ public class RemindThread extends Thread {
|
|||||||
public void setIsEnableUsageReminder(boolean isEnableUsageReminder) {
|
public void setIsEnableUsageReminder(boolean isEnableUsageReminder) {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
this.isEnableUsageReminder = isEnableUsageReminder;
|
this.isEnableUsageReminder = isEnableUsageReminder;
|
||||||
isReminding = (isEnableChargeReminder || isEnableUsageReminder) && isAppForeground;
|
isReminding = (isEnableChargeReminder || isEnableUsageReminder);
|
||||||
LogUtils.d(TAG, "设置耗电提醒开关:" + isEnableUsageReminder + ",全局提醒:" + isReminding);
|
LogUtils.d(TAG, "设置耗电提醒开关:" + isEnableUsageReminder + ",全局提醒:" + isReminding);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -538,7 +504,7 @@ public class RemindThread extends Thread {
|
|||||||
public void setIsEnableChargeReminder(boolean isEnableChargeReminder) {
|
public void setIsEnableChargeReminder(boolean isEnableChargeReminder) {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
this.isEnableChargeReminder = isEnableChargeReminder;
|
this.isEnableChargeReminder = isEnableChargeReminder;
|
||||||
isReminding = (isEnableChargeReminder || isEnableUsageReminder) && isAppForeground;
|
isReminding = (isEnableChargeReminder || isEnableUsageReminder);
|
||||||
LogUtils.d(TAG, "设置充电提醒开关:" + isEnableChargeReminder + ",全局提醒:" + isReminding);
|
LogUtils.d(TAG, "设置充电提醒开关:" + isEnableChargeReminder + ",全局提醒:" + isReminding);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -590,14 +556,8 @@ public class RemindThread extends Thread {
|
|||||||
|
|
||||||
public void setQuantityOfElectricity(int quantityOfElectricity) {
|
public void setQuantityOfElectricity(int quantityOfElectricity) {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
// 仅前台时更新电量,后台直接设为无效
|
|
||||||
if (isAppForeground) {
|
|
||||||
this.quantityOfElectricity = Math.min(Math.max(quantityOfElectricity, 0), 100);
|
this.quantityOfElectricity = Math.min(Math.max(quantityOfElectricity, 0), 100);
|
||||||
LogUtils.d(TAG, "更新当前电量:" + this.quantityOfElectricity);
|
LogUtils.d(TAG, "更新当前电量:" + this.quantityOfElectricity);
|
||||||
} else {
|
|
||||||
this.quantityOfElectricity = INVALID_BATTERY_VALUE;
|
|
||||||
LogUtils.w(TAG, "应用后台,不更新电量,标记为无效");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -618,7 +578,7 @@ public class RemindThread extends Thread {
|
|||||||
public boolean isRunning() {
|
public boolean isRunning() {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
boolean running = !isExist && isThreadStarted && isAlive();
|
boolean running = !isExist && isThreadStarted && isAlive();
|
||||||
LogUtils.d(TAG, "线程运行状态:" + running + "(退出:" + isExist + ",已启动:" + isThreadStarted + ",存活:" + isAlive() + ",前台:" + isAppForeground + ")");
|
LogUtils.d(TAG, "线程运行状态:" + running + "(退出:" + isExist + ",已启动:" + isThreadStarted + ",存活:" + isAlive() + ")");
|
||||||
return running;
|
return running;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -644,7 +604,6 @@ public class RemindThread extends Thread {
|
|||||||
"threadId=" + getId() +
|
"threadId=" + getId() +
|
||||||
", threadName='" + getName() + '\'' +
|
", threadName='" + getName() + '\'' +
|
||||||
", isRunning=" + isRunning() +
|
", isRunning=" + isRunning() +
|
||||||
", isForeground=" + isAppForeground() +
|
|
||||||
", isReminding=" + isReminding() +
|
", isReminding=" + isReminding() +
|
||||||
", chargeThreshold=" + getChargeReminderValue() +
|
", chargeThreshold=" + getChargeReminderValue() +
|
||||||
", usageThreshold=" + getUsageReminderValue() +
|
", usageThreshold=" + getUsageReminderValue() +
|
||||||
|
|||||||
Reference in New Issue
Block a user