handler, I want to run periodically

Jonghwan Seo picture Jonghwan Seo · Jul 31, 2013 · Viewed 11k times · Source

Using handler wants to run periodically The count is 0, if the countis 1, else Please fix this code.

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);

Answer

Raghunandan picture Raghunandan · Jul 31, 2013

Try the below

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

Also check this

Android Thread for a timer