TimerChangeManager.java
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.sw.laryngoscope.manager;
import android.content.Context;
import com.sw.laryngoscope.utils.Logger;
public class TimerChangeManager implements Runnable {
private static TimerChangeManager instance;
private Thread mThread = null;
private boolean isExitThread = false;
Context mContext;
private boolean isShowSet = false;
int hour = 0;
int min = 0;
long time = 0;
boolean is24HourFormat = false;
public static TimerChangeManager getInstance() {
if (instance == null) {
synchronized (TimerChangeManager.class) {
if (instance == null) {
instance = new TimerChangeManager();
}
}
}
return instance;
}
public void startThread(Context context) {
if ( mThread == null ) {
mThread = new Thread(this);
mThread.setName("TimerChangeManager");
mThread.start();
}
mContext = context;
/*HandlerThread ht = new HandlerThread("Thread");
ht.start();*/
}
public void exitThread() {
isExitThread = true;
}
@Override
public void run() {
while ( !isExitThread ) {
try {
incTime();
Thread.sleep(1 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public boolean isShowSet() {
return isShowSet;
}
public void setShowSet(boolean showSet) {
isShowSet = showSet;
}
public synchronized void setHour(int hour) {
this.hour = hour;
this.time = (hour * 60 + min) * 60 * 1000;
}
public synchronized void setMin(int min) {
this.min = min;
this.time = (hour * 60 + min) * 60 * 1000;
Logger.d(" setMin time " + time + " min " + min);
}
public String getTimeStr() {
if ( is24HourFormat ) {
return String.format("%02d:%02d", hour, min);
} else {
if ( hour == 24 ) {
return String.format("%02d:%02d", 0, min);
}
return String.format("%02d:%02d", hour >= 12 ? hour - 12 : hour, min);
}
}
public synchronized void incTime() {
time += 1000;
//将毫秒转换为秒
int second = Math.round(time * 1.0f / 1000);
//计算小时
hour = second / 3600;
//计算分钟
min = second % 3600 / 60;
Logger.d(" setMin1 time " + time + " min " + min);
}
public void setIs24HourFormat(boolean is24HourFormat) {
this.is24HourFormat = is24HourFormat;
}
}