线程在睡眠模式下停止工作

时间:2016-01-13 10:19:22

标签: android multithreading uiview

我正在更新线程中的计数器并且其工作正常。 但是当手机处于睡眠状态并且在获取唤醒锁之后运行活动时,线程不会使用计数器更新UI。

这是我正在更新视图值的函数。

private void updateTimeInBackground() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            startAlarm();
            int counter = MINUTE;
            // in this function Im using the runOnUiThread(...) to update the view and also used TextView.post(...)
            updateTextViewValue(counter);

            if (isDismissed)
                return;
            updateOverlayForAlert();

            counter = 0;
            int minutes = 1;
            vibrator.vibrate(1000);
            // in this function Im using the runOnUiThread(...) to update the view and also used TextView.post(...)
            updateTextViewValue(counter, minutes);
        }
    }).start();

有什么建议吗?还是提示?

2 个答案:

答案 0 :(得分:-1)

使用TimerTask,它将继续在后台运行。

希望这有帮助。

答案 1 :(得分:-1)

使用HandlerRunnable代替Thread

 final Handler handler=new Handler();

runnable=new Runnable() {

    @Override
    public void run() {
        handler.postDelayed(runnable, 5000);


        // your code
    }
};
runnable.run();
相关问题