处理程序,我想定期运行

Jon*_*Seo 5 android handler ui-thread

使用handler想要定期运行计数为0,如果countis为1,否则请修复此代码.

mRunnable = new Runnable(){
  @Override
  public void run() {
    if (count == 0) {
      setImage();
      count = 1;
    } else {
      weather = mContentResolver.getType(mUri);
      setWeather(weather);
      count = 0;
    }
  } 
};
mHandler = new Handler();
mHandler.postDelayed(mRunnable, 3000);
Run Code Online (Sandbox Code Playgroud)

Rag*_*dan 9

试试下面的内容

m_Handler = new Handler();
mRunnable = new Runnable(){
    @Override
    public void run() {
        if(count == 0){
            // do something
            count = 1;
        }
        else if (count==1){
            // do something
            count = 0;
        }
        m_Handler.postDelayed(mRunnable, 3000);// move this inside the run method
    } 
};
mRunnable.run(); // missing
Run Code Online (Sandbox Code Playgroud)

还检查一下

用于计时器的Android线程


And*_*ler 5

在这种情况下,您应该选择TimerTimerTask。下面是一个小例子:

//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {
        //Called each time when 1000 milliseconds (1 second) (the period parameter)
        //put your code here
    }

},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
3000);
Run Code Online (Sandbox Code Playgroud)

希望这是您所需要的。