我有一个变量在我的代码说它是“状态”。

我想在应用程序中根据这个变量值显示一些文本。这必须在特定的时间延迟下完成。

这就像,

检查状态变量值 显示一些文本 等待10秒 检查状态变量值 显示一些文本 等待15秒

等等。时间延迟可能会有所不同,在文本显示时设置。

我试过Thread。睡眠(时间延迟),它失败。还有更好的办法吗?


当前回答

对于使用Kotlin的人来说,inazaruk的答案将不起作用,IDE将需要初始化变量,因此我们将在一个单独的方法中使用它,而不是在Runnable中使用postDelayed。

Initialize your Runnable like this : private var myRunnable = Runnable { //Do some work //Magic happens here ↓ runDelayedHandler(1000) } Initialize your runDelayedHandler method like this : private fun runDelayedHandler(timeToWait : Long) { if (!keepRunning) { //Stop your handler handler.removeCallbacksAndMessages(null) //Do something here, this acts like onHandlerStop } else { //Keep it running handler.postDelayed(myRunnable, timeToWait) } } As you can see, this approach will make you able to control the lifetime of the task, keeping track of keepRunning and changing it during the lifetime of the application will do the job for you.

其他回答

定时器工作正常。在这里,我使用定时器搜索1.5s后的文本和更新UI。希望这能有所帮助。

private Timer _timer = new Timer();

_timer.schedule(new TimerTask() {
    @Override
    public void run() {
        // use runOnUiThread(Runnable action)
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                search();
            }
        });
    }
}, timeInterval);

对于使用Kotlin的人来说,inazaruk的答案将不起作用,IDE将需要初始化变量,因此我们将在一个单独的方法中使用它,而不是在Runnable中使用postDelayed。

Initialize your Runnable like this : private var myRunnable = Runnable { //Do some work //Magic happens here ↓ runDelayedHandler(1000) } Initialize your runDelayedHandler method like this : private fun runDelayedHandler(timeToWait : Long) { if (!keepRunning) { //Stop your handler handler.removeCallbacksAndMessages(null) //Do something here, this acts like onHandlerStop } else { //Keep it running handler.postDelayed(myRunnable, timeToWait) } } As you can see, this approach will make you able to control the lifetime of the task, keeping track of keepRunning and changing it during the lifetime of the application will do the job for you.

为此,您应该使用Handler的postDelayed函数。它将在主UI线程上以指定的延迟运行您的代码,因此您将能够更新UI控件。

private int mInterval = 5000; // 5 seconds by default, can be changed later
private Handler mHandler;

@Override
protected void onCreate(Bundle bundle) {

    // your code here

    mHandler = new Handler();
    startRepeatingTask();
}

@Override
public void onDestroy() {
    super.onDestroy();
    stopRepeatingTask();
}

Runnable mStatusChecker = new Runnable() {
    @Override 
    public void run() {
          try {
               updateStatus(); //this function can change value of mInterval.
          } finally {
               // 100% guarantee that this always happens, even if
               // your update method throws an exception
               mHandler.postDelayed(mStatusChecker, mInterval);
          }
    }
};

void startRepeatingTask() {
    mStatusChecker.run(); 
}

void stopRepeatingTask() {
    mHandler.removeCallbacks(mStatusChecker);
}

在我的例子中,如果以下条件之一为真,我就必须执行一个流程:如果前一个流程已经完成,或者如果5秒已经过去了。所以,我做了以下工作,并且工作得很好:

private Runnable mStatusChecker;
private Handler mHandler;

class {
method() {
  mStatusChecker = new Runnable() {
            int times = 0;
            @Override
            public void run() {
                if (times < 5) {
                    if (process1.isRead()) {
                        executeProcess2();
                    } else {
                        times++;
                        mHandler.postDelayed(mStatusChecker, 1000);
                    }
                } else {
                    executeProcess2();
                }
            }
        };

        mHandler = new Handler();
        startRepeatingTask();
}

    void startRepeatingTask() {
       mStatusChecker.run();
    }

    void stopRepeatingTask() {
        mHandler.removeCallbacks(mStatusChecker);
    }


}

如果process1被读取,它将执行process2。如果不是,则增加变量的时间,并使Handler在一秒钟后执行。它一直保持循环,直到process1被读取或times为5。当times为5时,这意味着5秒过去了,每一秒都执行process1.isRead()的if子句。

试试下面的例子,它可以工作!!

在onCreate()方法中使用[Handler],该方法使用postDelayed()方法,该方法会导致Runnable被添加到消息队列中,在给定的示例中,在指定的时间流逝后运行。1

参考以下代码:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
    //------------------
    //------------------
    android.os.Handler customHandler = new android.os.Handler();
            customHandler.postDelayed(updateTimerThread, 0);
}

private Runnable updateTimerThread = new Runnable()
{
        public void run()
        {
            //write here whaterver you want to repeat
            customHandler.postDelayed(this, 1000);
        }
};