android - Android中的线程没有停止问题
问题描述
我正在尝试通过 JNI 调用从后台服务显示 UI 上的进度条。该应用程序的流程是:
调用后台服务中的方法。这种方法给出了需要显示的数据。
使用 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);
如果布局已显示,则使用更新布局
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 方法更新视图,则窗口会显示两个进度条,旧布局的进度条和新进度条.
我期待的行为是旧的倒数计时器,线程不应该在新的更新视图布局上运行,而它显示新旧进度更新。
请指导我解决这个问题。
解决方案
推荐阅读
- delphi - Delphi 使用 RoGetAgileReference 的示例?
- android - Android Java - 从json对象响应中获取地名
- dart - Dart - 空运算符
- python - 加载 language_model_learner fastai 的问题
- python-3.x - 带有间隔 xticks 的堆积图
- reactjs - 如果在另一个库中重新导出 NPM 包组件会报错
- javascript - 如何通过javascript从html页面中的特定github txt文件中获取数据
- python - 在不使用python中的库函数的情况下将字符串转换为int或double
- bash - 在作为后台进程启动时处理 bash 脚本中的信号
- reactjs - 创建项目时未获得完整的项目数组。它只返回项目的名称