首页 > 解决方案 > Android中的线程没有停止问题

问题描述

我正在尝试通过 JNI 调用从后台服务显示 UI 上的进度条。该应用程序的流程是:

  1. 调用后台服务中的方法。这种方法给出了需要显示的数据。

  2. 使用 WindowManager 显示布局

    overLayLayout = (RelativeLayout)   LayoutInflater.from(context).inflate(R.layout.layout_body_template_1, null);
    myProgressBar = (ProgressBar) overLayLayout.findViewById(R.id.my_progressBar);
    windowManager.addView(v, layoutParams);
    
  3. 如果布局已显示,则使用更新布局

    overLayLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.layout_body_template_1, null);
    myProgressBar = (ProgressBar) overLayLayout.findViewById(R.id.my_progressBar);
    windowManager.updateViewLayout(v, params);
    

每当调用后台服务中的方法时,都会重复该流程。我在同一布局上显示水平进度条以显示音乐的进度。

        new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {

              mCountDownTimer = new CountDownTimer(totalDuration, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {

                        myProgressBar.setProgress((int) (i * 100 /  (totalDuration / 1000)));
                        currentMillis = currentMillis + 1000;

                    }


                }

                @Override
                public void onFinish() {

                    Log.v("Log_tag", "tick finished:");

                }

            };
      }
    });

我的问题是,当我显示布局时,它会播放进度条,但是如果新布局到达后台服务并且如果我使用 WindowManager 的 updateView 方法更新视图,则窗口会显示两个进度条,旧布局的进度条和新进度条.

我期待的行为是旧的倒数计时器,线程不应该在新的更新视图布局上运行,而它显示新旧进度更新。

请指导我解决这个问题。

标签: androidservicethreadpoolhandlercountdowntimer

解决方案


推荐阅读