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(); } }); }