Commit 9b326807 by patpat

Timer换成ScheduledExecutorService

parent e5b0579c
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 Timer();
timer.scheduleAtFixedRate(new TimerTask() {
final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
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) {
......
......@@ -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 mTimerExec: 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 = Timer()
mTimer!!.scheduleAtFixedRate(object: TimerTask() {
override fun run() {
if (mTimerExec == null) {
mTimerExec = Executors.newSingleThreadScheduledExecutor()
mTimerExec!!.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) {
......
......@@ -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 (sKeepAliveTimer == null) {
sKeepAliveTimer = new Timer();
sKeepAliveTimer.scheduleAtFixedRate(new TimerTask() {
if (sKeepAliveExec == null) {
sKeepAliveExec = Executors.newSingleThreadScheduledExecutor();
sKeepAliveExec.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);
}
}
......
......@@ -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 (mScanTimer == null) {
if (mScanExec == null) {
mAudioManager = (AudioManager) App.Companion.getDefault().getSystemService(Context.AUDIO_SERVICE);
mScanTimer = new Timer();
mScanTimer.scheduleAtFixedRate(new LoopTimerTask(), 0, 200);
mScanExec = Executors.newSingleThreadScheduledExecutor();
mScanExec.scheduleAtFixedRate(new LoopTimerTask(), 0, 200, TimeUnit.MILLISECONDS);
}
}
public void stop() {
mAudioManager = null;
if (mScanTimer != null) {
mScanTimer.cancel();
mScanTimer = null;
if (mScanExec != null) {
mScanExec.shutdown();
mScanExec = null;
}
}
......
......@@ -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 (mScanTimer == null) {
mScanTimer = new Timer();
mScanTimer.scheduleAtFixedRate(new LoopTimerTask(), 0, 200);
if (mScanExec == null) {
mScanExec = Executors.newSingleThreadScheduledExecutor();
mScanExec.scheduleAtFixedRate(new ScanRunnable(), 0, 200, TimeUnit.MILLISECONDS);
}
}
public void stop() {
if (mScanTimer != null) {
mScanTimer.cancel();
mScanTimer = null;
if (mScanExec != null) {
mScanExec.shutdown();
mScanExec = 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();
}
}
}
......
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 sign in to comment