在Android活动中,我使用CountDownTimer
,如下所示:
timer = new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
timeView.setText(String.format(Locale.getDefault(), "%ds", millisUntilFinished / 1000));
if (millisUntilFinished <= 10000) {
// Blinking text
ViewUtil.setBlinking(timeView,Animation.INFINITE);
} else {
timeView.clearAnimation();
}
}
public void onFinish() {
timeView.clearAnimation();
timeView.setText("0s");
}.start();
此计时器仅以秒为单位倒计时。在同一个活动中,我还使用PopupWindow
类显示了一个弹出窗口,如下所示:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ConstraintLayout mRelativeLayout = (ConstraintLayout) findViewById(R.id.task_layout);
View customView = inflater.inflate(R.layout.popup_window,mRelativeLayout,false);
mPopupWindow = new PopupWindow(
customView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
mPopupWindow.setElevation(5.0f);
mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
问题是,只要弹出窗口出现,CountDownTimer
就会以某种方式停止。我想要的是CountDownTimer
也会在弹出窗口出现且背景视图仍然更新时运行。
如何做到这一点?
答案 0 :(得分:1)
您正在UI线程中运行CountDownTimer
,当您弹出窗口时,该线程会转到该窗口,但是我搜索了PopupWindow
文档,但它的文档很少找不到
在后台使用CountDownTimer
解决问题。使用Asynctask
,Service
或Runnable