From cff607eabb11765e7da9d0ad5c96b735502622ce Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Thu, 4 Jul 2024 13:44:39 +0800 Subject: [PATCH] udpate --- .../utils/MessageSayHiNotifyManager.java | 62 +++++++------------ 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/common/src/main/java/com/yunbao/common/utils/MessageSayHiNotifyManager.java b/common/src/main/java/com/yunbao/common/utils/MessageSayHiNotifyManager.java index 8ade4772d..cbc4c2918 100644 --- a/common/src/main/java/com/yunbao/common/utils/MessageSayHiNotifyManager.java +++ b/common/src/main/java/com/yunbao/common/utils/MessageSayHiNotifyManager.java @@ -1,34 +1,31 @@ package com.yunbao.common.utils; -import android.app.Activity; import android.content.Context; +import android.os.Handler; +import android.os.Looper; import android.util.Log; -import com.yunbao.common.CommonAppContext; import com.yunbao.common.bean.BaseModel; import com.yunbao.common.bean.MessageSayHiBean; import com.yunbao.common.bean.MessageSayHiStartBean; import com.yunbao.common.bean.MessageUserInfoBean; -import com.yunbao.common.dialog.MessageChatNotifyDialog; import com.yunbao.common.dialog.MessageSayHiNotifyDialog; import com.yunbao.common.http.base.HttpCallback; import com.yunbao.common.http.live.LiveNetManager; -import com.yunbao.common.interfaces.OnItemClickListener; import java.util.ArrayList; import java.util.List; -import java.util.Timer; -import java.util.TimerTask; public class MessageSayHiNotifyManager { private static final String TAG = "打招呼定时器"; private static MessageSayHiNotifyManager instance; - private Timer timer = null; + private Handler timer = new Handler(Looper.getMainLooper()); private boolean isInit = false; private boolean isStopShow = false; private boolean isShowDialog = false; private boolean isRun = false; private boolean isCancelDialog = true; + private Runnable runnable; private MessageSayHiBean hiBean; private List startListNotifyList = new ArrayList<>(); private String lastActivity; @@ -53,12 +50,11 @@ public class MessageSayHiNotifyManager { return; } isCancelDialog = data.isCancelDialog(); - timer = new Timer(); - timer.schedule(createTask(), data.getNextTime() * 1000L); hiBean = new MessageSayHiBean(); hiBean.setModel("init"); hiBean.setNextTime(data.getNextTime()); hiBean.setOldTime(data.getNextTime()); + timer.postDelayed(createTask(), data.getNextTime() * 1000L); Log.i(TAG, "onSuccess: 初始化定时器,第一轮时间:" + data.getNextTime()); } @@ -83,13 +79,12 @@ public class MessageSayHiNotifyManager { Log.i(TAG, "onSuccess: 定时器为关,不再轮训 " + data); return; } - if (data.getStatus() == 0) { - timer = new Timer(); - timer.schedule(createTask(), data.getNextTime() * 1000L); - return; - } data.setOldTime(hiBean.getNextTime()); hiBean = data; + if (data.getUser() == null || data.getStatus()==0) { + timer.postDelayed(createTask(), data.getNextTime() * 1000L); + return; + } Log.i(TAG, "onSuccess: 展示弹框时间:" + data); showDialog(); } @@ -110,17 +105,16 @@ public class MessageSayHiNotifyManager { return; } if (hiBean == null) return; - Log.i(TAG, "reload: 重载定时器,当前Activity:" + AppManager.getInstance().getLastActivity() + "|等待:" + hiBean.getNextTime()); + Log.i(TAG, "reload: 重载定时器,当前Activity:" + AppManager.getInstance().getLastActivity() + "|等待:" + hiBean); isStopShow = false; if (timer != null) { - timer.cancel(); + timer.removeCallbacks(runnable); } - timer = new Timer(); if (hiBean.getStatus() == -2) { Log.i(TAG, "reload: -2的bean = " + hiBean); - timer.schedule(createTask(), hiBean.getOldTime() * 1000L); + timer.postDelayed(createTask(), hiBean.getOldTime() * 1000L); } else { - timer.schedule(createTask(), hiBean.getNextTime() * 1000L); + timer.postDelayed(createTask(), hiBean.getNextTime() * 1000L); } } @@ -133,41 +127,30 @@ public class MessageSayHiNotifyManager { } public void stop() { - Log.i(TAG, "stop: 停止定时器,当前Activity:" + AppManager.getInstance().getLastActivity()); + Log.i(TAG, "stop: 停止定时器,当前Activity:" + AppManager.getInstance().getLastActivity() + "|" + hiBean); isStopShow = true; - if(hiBean!=null) { + if (hiBean != null) { hiBean.setStatus(-2); } if (timer != null) { - timer.cancel(); - timer = null; + timer.removeCallbacks(runnable); } } - private TimerTask createTask() { + private Runnable createTask() { isRun = true; - return new TimerTask() { + runnable = new Runnable() { @Override public void run() { - if (timer != null) { - timer.cancel(); - } - cancel(); - if (hiBean.getStatus() == -2) { + if (hiBean != null && hiBean.getStatus() == -2 && !"init".equals(hiBean.getModel())) { showDialog(); return; } Log.i(TAG, "run: 定时器到点,启动下一轮"); next(); } - - @Override - public boolean cancel() { - isRun = false; - Log.i(TAG, "cancel: 定时器内部取消"); - return super.cancel(); - } }; + return runnable; } public void push(Context mContext, MessageUserInfoBean userInfo) { @@ -200,7 +183,7 @@ public class MessageSayHiNotifyManager { return; } if (isStopShow) { - Log.i(TAG, "showDialog: 非目标页面,停止展示"); + Log.i(TAG, "showDialog: 非目标页面,停止展示:" + hiBean); hiBean.setStatus(-2); lastActivity = null; isRun = false; @@ -235,8 +218,7 @@ public class MessageSayHiNotifyManager { Log.i(TAG, "onSuccess: 启动定时器,下一轮时间:" + hiBean.getNextTime() + "秒"); hiBean.setStatus(1); - timer = new Timer(); - timer.schedule(createTask(), hiBean.getNextTime() * 1000L); + timer.postDelayed(createTask(), hiBean.getNextTime() * 1000L); }).setCancelable(isCancelDialog) .showDialog(); }