6

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);
David
  • 3,971
  • 1
  • 26
  • 65
Jonghwan Seo
  • 71
  • 1
  • 1
  • 6

3 Answers3

9

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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
5

You should go for Timer and TimerTask in that case. Below is a small example:

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

Hope this is what you needed.

Android Killer
  • 18,174
  • 13
  • 67
  • 90
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);
  }};
}
QuokMoon
  • 4,387
  • 4
  • 26
  • 50