1:修改手机换绑倒计时添加管理类

This commit is contained in:
2025-12-19 18:41:29 +08:00
parent 5c607c58ae
commit 6bd210217a
3 changed files with 165 additions and 33 deletions

View File

@@ -13,10 +13,17 @@ import com.hjq.toast.ToastUtils;
import com.xscm.modulemain.activity.user.conacts.PhoneReplacementConacts; import com.xscm.modulemain.activity.user.conacts.PhoneReplacementConacts;
import com.xscm.modulemain.activity.user.presenter.PhoneReplacementPresenter; import com.xscm.modulemain.activity.user.presenter.PhoneReplacementPresenter;
import com.xscm.modulemain.BaseMvpActivity; import com.xscm.modulemain.BaseMvpActivity;
import com.xscm.modulemain.utils.CountDownManager;
import com.xscm.moduleutil.base.CommonAppContext; import com.xscm.moduleutil.base.CommonAppContext;
import com.xscm.moduleutil.color.ThemeableDrawableUtils; import com.xscm.moduleutil.color.ThemeableDrawableUtils;
import com.xscm.moduleutil.utils.ColorManager; import com.xscm.moduleutil.utils.ColorManager;
import com.xscm.moduleutil.utils.SpUtil; import com.xscm.moduleutil.utils.SpUtil;
import com.xscm.moduleutil.utils.TimeUtils;
import com.xscm.moduleutil.utils.TimerManager;
import java.util.concurrent.TimeUnit;
import kotlin.Unit;
/** /**
* @author * @author
@@ -24,7 +31,7 @@ import com.xscm.moduleutil.utils.SpUtil;
* @description: 手机换绑 * @description: 手机换绑
*/ */
public class PhoneReplacementActivity extends BaseMvpActivity<PhoneReplacementPresenter, ActivityPhoneReplacementBinding> implements PhoneReplacementConacts.View { public class PhoneReplacementActivity extends BaseMvpActivity<PhoneReplacementPresenter, ActivityPhoneReplacementBinding> implements PhoneReplacementConacts.View {
private CountDownTimer mTimer; private CountDownManager countDownManager;
private String mobile; private String mobile;
private String type;//1更换手机 2绑定手机 private String type;//1更换手机 2绑定手机
@@ -86,9 +93,9 @@ public class PhoneReplacementActivity extends BaseMvpActivity<PhoneReplacementPr
MvpPre.sendCode(mBinding.edPhone.getText().toString(), type, mBinding.tvSendCode); MvpPre.sendCode(mBinding.edPhone.getText().toString(), type, mBinding.tvSendCode);
} else if (view.getId() == R.id.btnSubmit) { } else if (view.getId() == R.id.btnSubmit) {
if (type.equals("1")){ if (type.equals("1")) {
if(mBinding.edTheOldPassword.getText().toString().isEmpty()){ if (mBinding.edTheOldPassword.getText().toString().isEmpty()) {
com.blankj.utilcode.util.ToastUtils.showShort("请输入旧手机验证码"); com.blankj.utilcode.util.ToastUtils.showShort("请输入旧手机验证码");
return; return;
} }
@@ -101,11 +108,11 @@ public class PhoneReplacementActivity extends BaseMvpActivity<PhoneReplacementPr
com.blankj.utilcode.util.ToastUtils.showShort("请输入验证码"); com.blankj.utilcode.util.ToastUtils.showShort("请输入验证码");
return; return;
} }
if (MvpPre!=null){ if (MvpPre != null) {
MvpPre.mobileView(SpUtil.getUserBean().getMobile(), mBinding.edPhone.getText().toString(),mBinding.edTheOldPassword.getText().toString(), mBinding.edPassword.getText().toString(), type); MvpPre.mobileView(SpUtil.getUserBean().getMobile(), mBinding.edPhone.getText().toString(), mBinding.edTheOldPassword.getText().toString(), mBinding.edPassword.getText().toString(), type);
} }
}else if (type.equals("2")){ } else if (type.equals("2")) {
if (mBinding.edPhone.getText().toString().isEmpty()) { if (mBinding.edPhone.getText().toString().isEmpty()) {
com.blankj.utilcode.util.ToastUtils.showShort("请输入手机号"); com.blankj.utilcode.util.ToastUtils.showShort("请输入手机号");
return; return;
@@ -114,8 +121,8 @@ public class PhoneReplacementActivity extends BaseMvpActivity<PhoneReplacementPr
com.blankj.utilcode.util.ToastUtils.showShort("请输入验证码"); com.blankj.utilcode.util.ToastUtils.showShort("请输入验证码");
return; return;
} }
if (MvpPre!=null){ if (MvpPre != null) {
MvpPre.mobileView("", mBinding.edPhone.getText().toString(), mBinding.edPassword.getText().toString(),"", type); MvpPre.mobileView("", mBinding.edPhone.getText().toString(), mBinding.edPassword.getText().toString(), "", type);
} }
} }
@@ -130,42 +137,52 @@ public class PhoneReplacementActivity extends BaseMvpActivity<PhoneReplacementPr
} }
} }
public void sendCodeSuccess2(String phoneNumber,TextView textView) { public void sendCodeSuccess2(String phoneNumber, TextView textView) {
com.blankj.utilcode.util.ToastUtils.showShort("短信验证码发送成功请注意查收"); com.blankj.utilcode.util.ToastUtils.showShort("短信验证码发送成功请注意查收");
textView.setEnabled(false); textView.setEnabled(false);
textView.setAlpha(0.5f); textView.setAlpha(0.5f);
releaseTimer();
if (mTimer != null) {
mTimer.cancel();
}
mTimer = new CountDownTimer(60000L, 1000L) {
@Override
public void onTick(long millisUntilFinished) {
if (textView != null) {
textView.setText(String.format("重新发送(%s", millisUntilFinished / 1000));
}
}
@Override // 获取CountDownManager单例实例
public void onFinish() { if (countDownManager == null) {
textView.setAlpha(1f); countDownManager = CountDownManager.Companion.getInstance();
textView.setEnabled(true); }
textView.setText("重新发送");
// 使用手机号作为tag来标识不同的倒计时任务
String tag = "countdown_" + textView.hashCode();
// 开始倒计时60秒
countDownManager.startCountDown(
tag,
60,
1000,
(remainingSeconds, tagParam) -> {
// 倒计时进行中
if (textView != null) {
textView.setText(String.format("重新发送(%s", remainingSeconds));
}
return null;
},
tagParam -> {
// 倒计时结束
if (textView != null) {
textView.setAlpha(1f);
textView.setEnabled(true);
textView.setText("重新发送");
}
return null;
} }
}; );
mTimer.start();
} }
private void releaseTimer() { private void releaseTimer() {
if (mTimer != null) { if (countDownManager != null) {
mTimer.cancel(); countDownManager.cancelAll();
mTimer = null;
} }
} }
@Override @Override
public void sendCodeSuccess1(String phoneNumber, TextView textView) { public void sendCodeSuccess1(String phoneNumber, TextView textView) {
sendCodeSuccess2(mBinding.edPhone.getText().toString(),textView); sendCodeSuccess2(mBinding.edPhone.getText().toString(), textView);
} }
@Override @Override
@@ -177,10 +194,10 @@ public class PhoneReplacementActivity extends BaseMvpActivity<PhoneReplacementPr
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
releaseTimer();
finish(); finish();
} else { } else {
ToastUtils.show(s); ToastUtils.show(s);
} }
} }
} }

View File

@@ -0,0 +1,114 @@
package com.xscm.modulemain.utils
import android.os.Handler
import android.os.Looper
/**
* 项目名称:羽声语音
* 时间2025/12/19 16:21
* 用途:
*/
class CountDownManager {
// 存储所有倒计时任务
private val timerMap = mutableMapOf<String, CountDownTask>()
// 单例模式
companion object {
private var instance: CountDownManager? = null
fun getInstance(): CountDownManager {
return instance ?: synchronized(this) {
instance ?: CountDownManager().also { instance = it }
}
}
}
data class CountDownTask(
val tag: String,
val totalSeconds: Long,
val interval: Long = 1000L,
var currentTime: Long = 0L,
var isRunning: Boolean = false,
val onTick: ((Long, String) -> Unit)? = null,
val onFinish: ((String) -> Unit)? = null
)
// 开始倒计时
fun startCountDown(
tag: String,
totalSeconds: Long,
interval: Long = 1000L,
onTick: ((Long, String) -> Unit)? = null,
onFinish: ((String) -> Unit)? = null
) {
// 如果已有相同tag的倒计时先取消
cancelCountDown(tag)
val task = CountDownTask(tag, totalSeconds, interval, totalSeconds, true, onTick, onFinish)
timerMap[tag] = task
val handler = Handler(Looper.getMainLooper())
val runnable = object : Runnable {
override fun run() {
val currentTask = timerMap[tag]
if (currentTask == null || !currentTask.isRunning) {
return
}
currentTask.currentTime -= 1
if (currentTask.currentTime > 0) {
onTick?.invoke(currentTask.currentTime, tag)
handler.postDelayed(this, interval)
} else {
currentTask.isRunning = false
timerMap.remove(tag)
onFinish?.invoke(tag)
}
}
}
handler.postDelayed(runnable, interval)
}
// 获取剩余时间
fun getRemainingTime(tag: String): Long {
return timerMap[tag]?.currentTime ?: 0L
}
// 检查是否在倒计时中
fun isCounting(tag: String): Boolean {
return timerMap[tag]?.isRunning ?: false
}
// 取消单个倒计时
fun cancelCountDown(tag: String) {
timerMap[tag]?.isRunning = false
timerMap.remove(tag)
}
// 取消所有倒计时
fun cancelAll() {
timerMap.values.forEach { it.isRunning = false }
timerMap.clear()
}
// 暂停倒计时(保存状态)
fun pauseCountDown(tag: String) {
timerMap[tag]?.isRunning = false
}
// 恢复倒计时
fun resumeCountDown(tag: String) {
val task = timerMap[tag] ?: return
if (task.currentTime > 0) {
startCountDown(
tag,
task.currentTime,
task.interval,
task.onTick,
task.onFinish
)
}
}
}

View File

@@ -11,6 +11,7 @@
android:id="@+id/parent" android:id="@+id/parent"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/dp_600" android:layout_height="@dimen/dp_600"
android:background="@mipmap/activity_bj"
> >
<TextView <TextView
@@ -30,7 +31,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/dp_600" android:layout_height="@dimen/dp_600"
android:layout_marginTop="30dp" android:layout_marginTop="30dp"
android:background="@mipmap/activity_bj"
app:layout_constraintTop_toBottomOf="@+id/title" app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
/> />