Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
E
Easy Cleaner Junk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
Easy Cleaner Junk
Commits
53667bb2
Commit
53667bb2
authored
Jul 12, 2024
by
leichao.gao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复定时器崩溃
parent
60bb8ad9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
33 deletions
+32
-33
MyTimerManager.java
...ava/com/test/easy/easycleanerjunk/fcm/MyTimerManager.java
+32
-33
No files found.
app/src/main/java/com/test/easy/easycleanerjunk/fcm/MyTimerManager.java
View file @
53667bb2
...
@@ -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
.
schedule
(
timerTask
,
initialDelay
,
intervalPeriod
);
timer
=
new
Timer
();
isTimerRunning
=
true
;
timer
.
schedule
(
timerTask
,
initialDelay
,
intervalPeriod
);
}
isTimerRunning
=
true
;
// 更新定时器状态为运行中
}
}
}
}
// 取消定时器
public
void
stopTimer
()
{
public
void
stopTimer
()
{
if
(
isTimerRunning
)
{
// 如果定时器在运行状态
synchronized
(
this
)
{
// 使用 synchronized 确保线程安全
if
(
timerTask
!=
null
)
{
if
(
isTimerRunning
)
{
timerTask
.
cancel
();
cancelExistingTimer
();
isTimerRunning
=
false
;
}
}
if
(
timer
!=
null
)
{
timer
.
cancel
();
timer
.
purge
();
// 清除所有取消的任务
}
isTimerRunning
=
false
;
// 更新定时器状态为停止
}
}
}
}
// 新增的方法,用于判断定时器是否在运行
private
void
cancelExistingTimer
()
{
if
(
timerTask
!=
null
)
{
timerTask
.
cancel
();
}
if
(
timer
!=
null
)
{
timer
.
cancel
();
timer
.
purge
();
}
}
public
boolean
isTimerRunning
()
{
public
boolean
isTimerRunning
()
{
return
isTimerRunning
;
return
isTimerRunning
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment