You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改Google MLSeries姿态检测代码:避免达标重复启动Activity

解决深蹲计数达标后重复启动Activity的问题

问题根源:当前代码在流模式下,每帧都会遍历repCounters并检查repsAfter == 3——只要计数维持在3,就会重复触发startActivity,导致目标Activity被多次打开。

解决方法

通过添加状态标志+调整触发时机,确保Activity只启动一次:

  1. 在类中添加一个布尔变量,标记是否已经触发过Activity启动:
private boolean hasLaunchedTargetActivity = false;
  1. 修改核心逻辑,仅在计数从2增加到3的瞬间执行启动操作,避免重复触发:
// Update {@link RepetitionCounter}s if {@code isStreamMode}.
if (isStreamMode) {
  // Feed pose to smoothing even if no pose found.
  classification = emaSmoothing.getSmoothedResult(classification);

  // Return early without updating repCounter if no pose found.
  if (pose.getAllPoseLandmarks().isEmpty()) {
    result.add(lastRepResult);
    return result;
  }

  for (RepetitionCounter repCounter : repCounters) {
    int repsBefore = repCounter.getNumRepeats();
    repsAfter = repCounter.addClassificationResult(classification);
    if (repsAfter > repsBefore) {
      // Play a fun beep when rep counter updates.
      ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
      tg.startTone(ToneGenerator.TONE_PROP_BEEP);
      lastRepResult = String.format(
              Locale.US, "%s : %d reps", repCounter.getClassName(), repsAfter);
      
      // 仅在计数刚达到3且未启动过Activity时执行
      if (repsAfter == 3 && !hasLaunchedTargetActivity) {
        hasLaunchedTargetActivity = true;
        // 停止计时
        swstopw2();
        tmClass.stopTimer();
        stopwatcher.swstop();
        context.startActivity(intent);
      }
      break;
    }
  }
  result.add(lastRepResult);
}

关键修改说明

  • 新增hasLaunchedTargetActivity标志,确保Activity只被启动一次
  • 将启动逻辑移到repsAfter > repsBefore分支内——只有当计数真正增加时才检查是否达标,避免每帧重复判断
  • 触发启动后立即将标志设为true,后续帧不再执行启动操作

如果需要支持用户重新开始深蹲计数,可以在重置计数器的逻辑中,把hasLaunchedTargetActivity重新设为false。

内容的提问来源于stack exchange,提问作者Nandatto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 13:10:29