在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
也会运行。
如何才能做到这一点?
你正在UI线程中运行CountDownTimer
,当你弹出窗口线程进入该窗口时,但是我通过PopupWindow
文档搜索但它有弱文档找不到多少
在背景中使用CountDownTimer
解决它。使用Asynctask
,Service
或Runnable