Commit 53667bb2 authored by leichao.gao's avatar leichao.gao

修复定时器崩溃

parent 60bb8ad9
...@@ -8,19 +8,15 @@ import java.util.Timer; ...@@ -8,19 +8,15 @@ import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
public class MyTimerManager { public class MyTimerManager {
// 单例对象
private static MyTimerManager instance; private static MyTimerManager instance;
private Timer timer; private Timer timer;
private boolean isTimerRunning;
private TimerTask timerTask; private TimerTask timerTask;
private boolean isTimerRunning; // 新增的成员变量,用于跟踪定时器状态
// 私有构造函数
private MyTimerManager() { private MyTimerManager() {
// 初始化成员变量
isTimerRunning = false; isTimerRunning = false;
} }
// 获取单例对象的方法
public static synchronized MyTimerManager getInstance() { public static synchronized MyTimerManager getInstance() {
if (instance == null) { if (instance == null) {
instance = new MyTimerManager(); instance = new MyTimerManager();
...@@ -28,44 +24,47 @@ public class MyTimerManager { ...@@ -28,44 +24,47 @@ public class MyTimerManager {
return instance; return instance;
} }
// 启动定时器
public void startTimer(long initialDelay, long intervalPeriod) { public void startTimer(long initialDelay, long intervalPeriod) {
if (!isTimerRunning) { // 如果定时器不在运行状态 synchronized (this) { // 使用 synchronized 确保线程安全
if (timer != null) { if (!isTimerRunning) {
timer.cancel(); cancelExistingTimer(); // 取消现有的定时器和任务
timer = null; // 创建新的 TimerTask 实例并调度
} timer = new Timer();
timerTask = new TimerTask() { timerTask = new TimerTask() {
@Override @Override
public void run() { public void run() {
Log.d("glc","定时器执行"); Log.d("glc", "定时器执行");
if (ActionBroadcast.mIsScreenOn && !ActionBroadcast.isLock && MyApplication.PAUSED_VALUE != 1) { if (ActionBroadcast.mIsScreenOn && !ActionBroadcast.isLock && MyApplication.PAUSED_VALUE != 1) {
Log.d("glc","定时器执行条件满足"); Log.d("glc", "定时器执行条件满足");
NotificationUtil.sendNotification(MyApplication.context); NotificationUtil.sendNotification(MyApplication.context);
} }
} }
}; };
timer = new Timer();
timer.schedule(timerTask, initialDelay, intervalPeriod); timer.schedule(timerTask, initialDelay, intervalPeriod);
isTimerRunning = true; // 更新定时器状态为运行中 isTimerRunning = true;
}
} }
} }
// 取消定时器
public void stopTimer() { public void stopTimer() {
if (isTimerRunning) { // 如果定时器在运行状态 synchronized (this) { // 使用 synchronized 确保线程安全
if (isTimerRunning) {
cancelExistingTimer();
isTimerRunning = false;
}
}
}
private void cancelExistingTimer() {
if (timerTask != null) { if (timerTask != null) {
timerTask.cancel(); timerTask.cancel();
} }
if (timer != null) { if (timer != null) {
timer.cancel(); timer.cancel();
timer.purge(); // 清除所有取消的任务 timer.purge();
}
isTimerRunning = false; // 更新定时器状态为停止
} }
} }
// 新增的方法,用于判断定时器是否在运行
public boolean isTimerRunning() { public boolean isTimerRunning() {
return isTimerRunning; return isTimerRunning;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment