6

使用处理程序要定期运行计数为0,如果计数为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);
4

3 回答 3

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

还要检查这个

用于计时器的 Android 线程

于 2013-07-31T05:36:46.437 回答
4

在这种情况下,您应该选择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);

希望这是你需要的。

于 2013-07-31T05:07:07.597 回答
0
 private Handler handler = new Handler();
 handler.post(timedTask);

private Runnable timedTask = new Runnable(){

  @Override
  public void run() {
   // TODO Auto-generated method stub
   cnt++;
   if(cnt==0)
   {
    //set you view or update your 
   }

   handler.postDelayed(timedTask, 500);
  }};
}
于 2013-07-31T05:14:00.513 回答