handler, I want to run periodically
Asked Answered
Y

3

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);
Young answered 31/7, 2013 at 5:0 Comment(10)
what's wrong with the code? Did you forget to call mRunnable.run()?Polyamide
what is the problem buddy >?Chromatography
@Polyamide uhm... no problem but This code does not execute periodically repeated.Young
@AndroidKiller no problem but This code does not execute periodically repeated.Young
@JonghwanSeo did you call the run method?Polyamide
What if I don't want to fix the code.Synsepalous
@Cole"Cole9"Johnson he is the boss and he is ordering us to do so. So do it. :p ;). Else he will cut our salary.Chromatography
@AndroidKiller what? You mean the rations fed to me by my parental units? Oh please no!Synsepalous
@JonghwanSeo https://mcmap.net/q/1769931/-android-thread-for-a-timer/…. you can use a handler, a timer, a coundowntimer.Polyamide
@JonghwanSeo check my post. it should workPolyamide
P
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

Polyamide answered 31/7, 2013 at 5:36 Comment(3)
!!!!!!!!!!!!!!!! Thank!!!!!!!!!!!!U!!!!!!!!!!!!!!!!!!!! i love u so much @PolyamideYoung
wouldn't m_Handler.post(mRunnable) be better than mRunnable.run() ?Colostrum
@Colostrum yup you could do that( the android way)Polyamide
C
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.

Chromatography answered 31/7, 2013 at 5:7 Comment(8)
@Polyamide i don't think handler is having any function to repeat the code periodically. Or is thr any ?Chromatography
https://mcmap.net/q/1769931/-android-thread-for-a-timer/…. check this you can use a handler also. Also you cannot update ui from the timer as it runs on a different threadPolyamide
setImage(); is probably setting image ie updating uiPolyamide
In that case you can use runOnUiThread() inside setImage() method itself.Chromatography
Handler we can use for sure as @altaf told in his answer. But that code looks repeatedly calling postDelayed which looks little weird but you can take my answer as another way of doing that. :)Chromatography
everybody thank you so much .. i'm sorry that i dont speak english very wellYoung
@JonghwanSeo come on don't worry about that. This is not an english learning forum. So did u make it work in anyway ?Chromatography
@AndroidKiller no .. yet I will make. Program that converts alternating icon and In fact, I'm a beginner Android so it hard.Young
C
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);
  }};
}
Chrisy answered 31/7, 2013 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.