From 87e3dddac52b528e3840ebc1d7fd7e89dd10cfa7 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Thu, 29 Dec 2022 16:34:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8B=E6=92=AD=E5=90=8E?= =?UTF-8?q?=E9=9A=8F=E5=8D=B3PK=E6=B2=A1=E6=9C=89=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/manager/RandomPkManager.java | 56 ++++++++++++++----- .../live/activity/LiveRyAnchorActivity.java | 1 + 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/common/src/main/java/com/yunbao/common/manager/RandomPkManager.java b/common/src/main/java/com/yunbao/common/manager/RandomPkManager.java index a48203591..cd4a847b5 100644 --- a/common/src/main/java/com/yunbao/common/manager/RandomPkManager.java +++ b/common/src/main/java/com/yunbao/common/manager/RandomPkManager.java @@ -37,6 +37,8 @@ public class RandomPkManager { private String pkUid;//对方房间ID public static String debugUid; private RankPkInfoBean rankPkInfoBean;//排位赛信息 + private int exitTimer;//退出倒计时 + private TimerTask exitTask = null;//退出任务 Handler handler = new Handler(Looper.getMainLooper()); private RandomPkManager() { @@ -118,6 +120,8 @@ public class RandomPkManager { return String.format(Locale.CHINA, "%02d:%02d", min, sec); } + private Runnable pkRunnable = this::next; + /** * 对方拒绝,开始下一轮匹配 */ @@ -125,11 +129,11 @@ public class RandomPkManager { if (status != PK_STATUS_DEFAULT && status != PK_STATUS_REFUSE) { return; } - new Handler(Looper.getMainLooper()).postDelayed(this::next, 5000); + handler.postDelayed(pkRunnable, 5000); } private void next() { - if (task==null) { + if (task == null) { return; } status = PK_STATUS_REQUEST; @@ -243,19 +247,9 @@ public class RandomPkManager { } } - private int exitTimer; - /** - * 退出匹配 - */ - public void exitPk() { - if (status == PK_STATUS_EXIT_ING) { - return; - } - callEndPkStart(); - status = PK_STATUS_EXIT_ING; - exitTimer = 11; - new Timer().schedule(new TimerTask() { + private TimerTask createExitTask() { + return new TimerTask() { Handler handler = new Handler(Looper.getMainLooper()); @@ -269,10 +263,25 @@ public class RandomPkManager { end(); callEndPkSuccess(); cancel(); + exitTask = null; } }); } - }, 0, 1000); + }; + } + + /** + * 退出匹配 + */ + public void exitPk() { + if (status == PK_STATUS_EXIT_ING) { + return; + } + callEndPkStart(); + status = PK_STATUS_EXIT_ING; + exitTimer = 11; + exitTask = createExitTask(); + new Timer().schedule(exitTask, 0, 1000); } /** @@ -387,6 +396,23 @@ public class RandomPkManager { return exitTimer + ""; } + /** + * 清空状态,下播时调用 + */ + public void release() { + status = PK_STATUS_REQUEST; + if (task != null) { + task.cancel(); + } + if (exitTask != null) { + exitTask.cancel(); + } + randomPkTimer.clear(); + rankPkInfoBean = null; + pkUid = null; + handler.removeCallbacks(pkRunnable); + } + public static abstract class OnRandomPkTimer { /** diff --git a/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java b/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java index 8a1dc02df..f4f8b8178 100644 --- a/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java +++ b/live/src/main/java/com/yunbao/live/activity/LiveRyAnchorActivity.java @@ -843,6 +843,7 @@ public class LiveRyAnchorActivity extends LiveActivity implements LiveFunctionCl PKing = false; isDRPK = 0; endLive(); + RandomPkManager.getInstance().release(); } }); }