Commit 1fb75bf1 by patpat

修改TimerHelper回调

parent 6fe0f7d4
......@@ -76,7 +76,7 @@ class WifiAction private constructor() : Action(ActionEnum.CONFIG_WIFI.name) {
}
EventBus.getDefault().post(WifiStateEvent(30, "正在连接Wifi"))
TimerHelper.loop({ _, isLastTime ->
TimerHelper.loop({ id, isLastTime ->
if (isLastTime) {
if (state == State.STARTED) {
state = State.FAIL
......@@ -97,11 +97,10 @@ class WifiAction private constructor() : Action(ActionEnum.CONFIG_WIFI.name) {
state = State.INITED
EventBus.getDefault().post(WifiStateEvent(-1))
}, 3000)
return@loop false
TimerHelper.cancel(id)
}
}
}
return@loop true
}, 1000, 10)
} catch (e: Exception) {
......
......@@ -93,7 +93,7 @@ class MainActivity : BaseActivity() {
}
private fun initTimer() {
TimerHelper.loop(Runnable {
TimerHelper.loop({ _, _ ->
mHandler.post {
_wifi.text = "Wifi Mac: ${WifiHelpler.getMacAddress()} SSID: ${WifiHelpler.getSSID()} IP: ${WifiHelpler.getIpString()}"
_time.text = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date())
......
......@@ -47,18 +47,17 @@ object TimerHelper {
return id
}
fun loop(runnable: Runnable, period: Long): Long {
return loop(runnable, period, -1)
fun loop(task: LoopTask, period: Long): Long {
return loop(task, period, -1)
}
fun loop(runnable: Runnable, period: Long, times: Int): Long {
return loop({ _: Long, _: Boolean ->
runnable.run()
true
fun loop(task: LoopTask, period: Long, times: Int): Long {
return loop({ id: Long, isLastTime: Boolean ->
task.run(id, isLastTime)
}, period, times)
}
fun loop(runnable: (id: Long, isLastTime: Boolean) -> Boolean, period: Long, times: Int = -1): Long {
fun loop(runnable: (id: Long, isLastTime: Boolean) -> Unit, period: Long, times: Int = -1): Long {
val id = mId
mFutureList.put(id, mScheduledExecutorService.scheduleAtFixedRate(object: Runnable {
var vTimes = 0
......@@ -71,9 +70,7 @@ object TimerHelper {
}
}
try {
if (!runnable(id, vTimes == times)) {
cancel(id)
}
runnable(id, vTimes == times)
} catch (e: Exception) {
Log.w(TAG, "loop id: $id times: $vTimes error: ${e.message}")
}
......@@ -81,4 +78,8 @@ object TimerHelper {
}, 0, period, TimeUnit.MILLISECONDS))
return id
}
interface LoopTask {
fun run(id: Long, isLastTime: Boolean)
}
}
\ No newline at end of file
......@@ -59,9 +59,9 @@ public class MainWebSocket extends WebSocketClient {
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTING));
sInstance = new MainWebSocket(new URI(String.format(AppConfig.getMainWebSocketServerUrl(), sDeviceSN)));
sInstance.connect();
TimerHelper.INSTANCE.loop(new Runnable() {
TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
@Override
public void run() {
public void run(long id, boolean isLastTime) {
if (sInstance.isClosed()) {
sInstance.reconnect();
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.RECONNECTING));
......
......@@ -17,7 +17,7 @@ object ButtonTask {
fun start() {
if (mTimerId < 0) {
mAudioManager = App.getDefault().getSystemService(Context.AUDIO_SERVICE) as AudioManager
mTimerId = TimerHelper.loop(Runnable {
mTimerId = TimerHelper.loop({ _, _ ->
val buf = ByteArray(5)
libszxb.devicekey(buf)
// Log.i(TAG, String.format("%02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3]));
......
......@@ -46,13 +46,12 @@ public class QRCodeTask {
if (mScanAsyncTask == null) {
mScanAsyncTask = new ScanAsyncTask();
TimerHelper.INSTANCE.loop(new Function2<Long, Boolean, Boolean>() {
TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
@Override
public Boolean invoke(Long id, Boolean isLastTime) {
if (mScanAsyncTask == null) return false;
public void run(long id, boolean isLastTime) {
if (mScanAsyncTask == null) return;
mScanAsyncTask.run();
return true;
}
}, 200, -1);
}
......
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