diff --git a/app/src/main/assets/print_demo.html b/app/src/main/assets/print_demo.html index 572cf1f..34adcc1 100644 --- a/app/src/main/assets/print_demo.html +++ b/app/src/main/assets/print_demo.html @@ -204,7 +204,7 @@
@@ -657,12 +657,18 @@ // 修复 PTT 按键逻辑:支持 TouchEvents 并处理异常中断 if (pttBtn) { var isDown = false; + var lastTouchTime = 0; var startSpeak = function(e) { if (pttBtn.disabled) return; e.preventDefault(); + + var now = Date.now(); + if (now - lastTouchTime < 300) return; // 物理防抖:300ms 内禁止重复触发按下 + if (!isDown) { isDown = true; + lastTouchTime = now; pttBtn.style.background = "#b91c1c"; pttBtn.innerText = "正在通话..."; if (window.android && window.android.pttSpeakPlay) { @@ -674,13 +680,17 @@ var stopSpeak = function(e) { if (isDown) { - isDown = false; - pttBtn.style.background = "#ef4444"; - pttBtn.innerText = "长按对讲 (PTT)"; - if (window.android && window.android.pttSpeakRelease) { - window.android.pttSpeakRelease(); - } - showPttResult("释放话权"); + // 延迟 50ms 再处理释放,确保即使在极速点击下,Down 指令也先到达原生层 + setTimeout(function() { + if (!isDown) return; + isDown = false; + pttBtn.style.background = "#ef4444"; + pttBtn.innerText = "长按对讲 (PTT)"; + if (window.android && window.android.pttSpeakRelease) { + window.android.pttSpeakRelease(); + } + showPttResult("释放话权"); + }, 50); } }; diff --git a/app/src/main/java/com/stand/standapp/AndroidInterface.kt b/app/src/main/java/com/stand/standapp/AndroidInterface.kt index 5197f41..689b7b1 100644 --- a/app/src/main/java/com/stand/standapp/AndroidInterface.kt +++ b/app/src/main/java/com/stand/standapp/AndroidInterface.kt @@ -17,14 +17,8 @@ import org.json.JSONObject class AndroidInterface(private val activity: MainActivity) { private var isSpeaking = false - - /** - * PTT 登录 - */ - @JavascriptInterface - fun pttLogin(account: String, pwd: String, ip: String) { - SendAtUtil.toLogin(account, pwd, ip, 1, 1) - } + private var lastSpeakTime = 0L + private val MIN_SPEAK_DURATION = 200L // 最小通话时长 200ms /** * PTT 开始说话 (带状态保护) @@ -33,6 +27,7 @@ class AndroidInterface(private val activity: MainActivity) { fun pttSpeakPlay() { if (!isSpeaking) { isSpeaking = true + lastSpeakTime = System.currentTimeMillis() SendAtUtil.speakPlay() Timber.tag("PTT").d("Speak Play") } @@ -43,10 +38,28 @@ class AndroidInterface(private val activity: MainActivity) { */ @JavascriptInterface fun pttSpeakRelease() { + if (isSpeaking) { + val now = System.currentTimeMillis() + val duration = now - lastSpeakTime + + if (duration < MIN_SPEAK_DURATION) { + // 如果按得太快,延迟释放 + val delay = MIN_SPEAK_DURATION - duration + Timber.tag("PTT").d("Speak duration too short ($duration ms), delaying release by $delay ms") + Handler(Looper.getMainLooper()).postDelayed({ + realRelease() + }, delay) + } else { + realRelease() + } + } + } + + private fun realRelease() { if (isSpeaking) { isSpeaking = false SendAtUtil.speakRelease() - Timber.tag("PTT").d("Speak Release") + Timber.tag("PTT").d("Real Speak Release") } }