Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
9b326807
authored
Aug 02, 2019
by
patpat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Timer换成ScheduledExecutorService
parent
e5b0579c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
83 additions
and
56 deletions
+83
-56
app/src/main/java/com/bgycc/smartcanteen/action/Action.java
+23
-19
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.kt
+16
-9
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
+17
-9
app/src/main/java/com/bgycc/smartcanteen/task/ButtonTask.java
+10
-7
app/src/main/java/com/bgycc/smartcanteen/task/QRCodeTask.java
+17
-12
No files found.
app/src/main/java/com/bgycc/smartcanteen/action/Action.java
View file @
9b326807
package
com
.
bgycc
.
smartcanteen
.
action
;
import
com.bgycc.smartcanteen.event.PayStateEvent
;
import
com.bgycc.smartcanteen.server.websocket.MainWebSocket
;
import
org.greenrobot.eventbus.EventBus
;
import
org.json.JSONObject
;
import
java.util.Timer
;
import
java.util.TimerTask
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.TimeUnit
;
public
abstract
class
Action
{
...
...
@@ -58,29 +54,37 @@ public abstract class Action {
}
protected
void
timeout
(
final
Runnable
runnable
,
long
ms
)
{
new
Timer
().
schedule
(
new
TimerTask
()
{
Executors
.
newSingleThreadScheduledExecutor
().
schedule
(
new
Runnable
()
{
@Override
public
void
run
()
{
if
(
runnable
!=
null
)
runnable
.
run
();
try
{
if
(
runnable
!=
null
)
runnable
.
run
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
},
ms
);
},
ms
,
TimeUnit
.
MILLISECONDS
);
}
protected
void
loop
(
final
Runnable
runnable
,
final
Runnable
endRunnable
,
final
int
times
,
long
period
)
{
final
Timer
timer
=
new
Time
r
();
timer
.
scheduleAtFixedRate
(
new
TimerTask
()
{
final
ScheduledExecutorService
exec
=
Executors
.
newSingleThreadScheduledExecuto
r
();
exec
.
scheduleAtFixedRate
(
new
Runnable
()
{
int
count
=
0
;
@Override
public
void
run
()
{
if
(
count
>=
times
)
{
if
(
endRunnable
!=
null
)
endRunnable
.
run
();
timer
.
cancel
();
return
;
try
{
if
(
count
>=
times
)
{
if
(
endRunnable
!=
null
)
endRunnable
.
run
();
exec
.
shutdown
();
return
;
}
count
++;
if
(
runnable
!=
null
)
runnable
.
run
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
count
++;
if
(
runnable
!=
null
)
runnable
.
run
();
}
},
0
,
period
);
},
0
,
period
,
TimeUnit
.
MILLISECONDS
);
}
protected
void
success
(
String
message
)
{
...
...
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.kt
View file @
9b326807
...
...
@@ -4,6 +4,7 @@ import android.media.AudioManager
import
android.media.SoundPool
import
android.os.Bundle
import
android.os.Handler
import
android.util.Log
import
android.view.View
import
android.view.animation.Animation
import
android.view.animation.RotateAnimation
...
...
@@ -28,7 +29,11 @@ import org.greenrobot.eventbus.ThreadMode
import
java.text.SimpleDateFormat
import
java.util.*
import
kotlinx.android.synthetic.main.activity_main.*
import
java.lang.Exception
import
java.text.DateFormat
import
java.util.concurrent.Executors
import
java.util.concurrent.ScheduledExecutorService
import
java.util.concurrent.TimeUnit
class
MainActivity
:
BaseActivity
()
{
...
...
@@ -40,7 +45,7 @@ class MainActivity : BaseActivity() {
private
var
mBeepSoundId
=
0
private
var
mPaySuccessSoundId
=
0
private
var
mPayFailSoundId
=
0
private
var
mTimer
:
Timer
?
=
null
private
var
mTimer
Exec
:
ScheduledExecutorService
?
=
null
private
val
mHandler
=
Handler
()
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
...
...
@@ -88,22 +93,24 @@ class MainActivity : BaseActivity() {
}
private
fun
initTimer
()
{
if
(
mTimer
==
null
)
{
mTimer
=
Time
r
()
mTimer
!!
.
scheduleAtFixedRate
(
object
:
TimerTask
()
{
override
fun
run
()
{
if
(
mTimer
Exec
==
null
)
{
mTimer
Exec
=
Executors
.
newSingleThreadScheduledExecuto
r
()
mTimer
Exec
!!
.
scheduleAtFixedRate
(
{
try
{
mHandler
.
post
{
_time
.
text
=
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
,
Locale
.
getDefault
()).
format
(
Date
())
}
}
catch
(
e
:
Exception
)
{
Log
.
e
(
TAG
,
"TimerExec error"
)
e
.
printStackTrace
()
}
},
0
,
1000
)
},
0
,
1000
,
TimeUnit
.
MILLISECONDS
)
}
}
private
fun
uninitTimer
()
{
mTimer
?:
return
mTimer
!!
.
cancel
()
mTimer
=
null
mTimerExec
?.
shutdown
()
mTimerExec
=
null
}
fun
printQRCode
(
event
:
QRCodeEvent
)
{
...
...
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
View file @
9b326807
...
...
@@ -22,6 +22,9 @@ import java.net.URI;
import
java.net.URISyntaxException
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.TimeUnit
;
public
class
MainWebSocket
extends
WebSocketClient
{
...
...
@@ -45,7 +48,7 @@ public class MainWebSocket extends WebSocketClient {
private
static
MainWebSocket
sInstance
;
private
static
String
sDeviceSN
;
private
static
Timer
sKeepAliveTimer
;
private
static
ScheduledExecutorService
sKeepAliveExec
;
public
static
void
initialize
()
{
if
(
sInstance
==
null
)
{
...
...
@@ -60,18 +63,23 @@ public class MainWebSocket extends WebSocketClient {
e
.
printStackTrace
();
}
}
if
(
sKeepAlive
Timer
==
null
)
{
sKeepAlive
Timer
=
new
Time
r
();
sKeepAlive
Timer
.
scheduleAtFixedRate
(
new
TimerTask
()
{
if
(
sKeepAlive
Exec
==
null
)
{
sKeepAlive
Exec
=
Executors
.
newSingleThreadScheduledExecuto
r
();
sKeepAlive
Exec
.
scheduleAtFixedRate
(
new
Runnable
()
{
@Override
public
void
run
()
{
if
(
sInstance
==
null
)
return
;
if
(
sInstance
.
isClosed
())
{
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
RECONNECTING
));
sInstance
.
reconnect
();
try
{
if
(
sInstance
==
null
)
return
;
if
(
sInstance
.
isClosed
())
{
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
RECONNECTING
));
sInstance
.
reconnect
();
}
}
catch
(
Exception
e
)
{
Log
.
e
(
TAG
,
"sKeepAliveExec error"
);
e
.
printStackTrace
();
}
}
},
0
,
1000
);
},
0
,
1000
,
TimeUnit
.
MILLISECONDS
);
}
}
...
...
app/src/main/java/com/bgycc/smartcanteen/task/ButtonTask.java
View file @
9b326807
...
...
@@ -8,6 +8,9 @@ import com.szxb.jni.libszxb;
import
java.util.Timer
;
import
java.util.TimerTask
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.TimeUnit
;
public
class
ButtonTask
{
...
...
@@ -22,24 +25,24 @@ public class ButtonTask {
return
sInstance
;
}
private
Timer
mScanTimer
;
private
ScheduledExecutorService
mScanExec
;
private
AudioManager
mAudioManager
;
private
ButtonTask
()
{}
public
void
start
()
{
if
(
mScan
Timer
==
null
)
{
if
(
mScan
Exec
==
null
)
{
mAudioManager
=
(
AudioManager
)
App
.
Companion
.
getDefault
().
getSystemService
(
Context
.
AUDIO_SERVICE
);
mScan
Timer
=
new
Time
r
();
mScan
Timer
.
scheduleAtFixedRate
(
new
LoopTimerTask
(),
0
,
200
);
mScan
Exec
=
Executors
.
newSingleThreadScheduledExecuto
r
();
mScan
Exec
.
scheduleAtFixedRate
(
new
LoopTimerTask
(),
0
,
200
,
TimeUnit
.
MILLISECONDS
);
}
}
public
void
stop
()
{
mAudioManager
=
null
;
if
(
mScan
Timer
!=
null
)
{
mScan
Timer
.
cancel
();
mScan
Timer
=
null
;
if
(
mScan
Exec
!=
null
)
{
mScan
Exec
.
shutdown
();
mScan
Exec
=
null
;
}
}
...
...
app/src/main/java/com/bgycc/smartcanteen/task/QRCodeTask.java
View file @
9b326807
...
...
@@ -5,13 +5,13 @@ import com.bgycc.smartcanteen.QRCodeEvent;
import
com.bgycc.smartcanteen.action.*
;
import
com.bgycc.smartcanteen.event.LogEvent
;
import
com.bgycc.smartcanteen.server.websocket.MainWebSocket
;
import
com.bgycc.smartcanteen.util.StringUtil
;
import
com.szxb.jni.libszxb
;
import
org.greenrobot.eventbus.EventBus
;
import
org.json.JSONObject
;
import
java.util.Timer
;
import
java.util.TimerTask
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ScheduledExecutorService
;
import
java.util.concurrent.TimeUnit
;
import
java.util.regex.Pattern
;
public
class
QRCodeTask
{
...
...
@@ -31,7 +31,7 @@ public class QRCodeTask {
return
sInstance
;
}
private
Timer
mScanTimer
;
private
ScheduledExecutorService
mScanExec
;
private
QRCodeTask
()
{}
...
...
@@ -42,20 +42,20 @@ public class QRCodeTask {
public
void
start
()
{
if
(!
support
())
return
;
if
(
mScan
Timer
==
null
)
{
mScan
Timer
=
new
Time
r
();
mScan
Timer
.
scheduleAtFixedRate
(
new
LoopTimerTask
(),
0
,
200
);
if
(
mScan
Exec
==
null
)
{
mScan
Exec
=
Executors
.
newSingleThreadScheduledExecuto
r
();
mScan
Exec
.
scheduleAtFixedRate
(
new
ScanRunnable
(),
0
,
200
,
TimeUnit
.
MILLISECONDS
);
}
}
public
void
stop
()
{
if
(
mScan
Timer
!=
null
)
{
mScan
Timer
.
cancel
();
mScan
Timer
=
null
;
if
(
mScan
Exec
!=
null
)
{
mScan
Exec
.
shutdown
();
mScan
Exec
=
null
;
}
}
private
class
LoopTimerTask
extends
TimerTask
{
private
class
ScanRunnable
implements
Runnable
{
int
lastLen
=
-
1
;
byte
[]
lastBuf
;
...
...
@@ -137,7 +137,12 @@ public class QRCodeTask {
};
@Override
public
void
run
()
{
asyncTask
.
run
();
try
{
asyncTask
.
run
();
}
catch
(
Exception
e
)
{
Log
.
e
(
TAG
,
"ScanRunnable error"
);
e
.
printStackTrace
();
}
}
}
...
...
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