Home » Android » thread safety – How to call method after every 20 seconds in Android

thread safety – How to call method after every 20 seconds in Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to write an Android application which when opened call a method after 20 seconds then auto call method every 10 seconds.

Any thoughts on how to proceed?

How to&Answers:

You could do it with a single Handler like this:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
   @Override
   public void run() {
       //Do something after 20 seconds
   }
}, 20000);  //the time is in miliseconds

And to repeate the same every 10 seconds you could add the following line in the same method

handler.postDelayed(this, 10000);

Like this:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
   @Override
   public void run() {
       //Do something after 20 seconds
       handler.postDelayed(this, 10000);
   }
}, 20000);  //the time is in miliseconds

Answer:

CountDownTimer cdt;

cdt = new CountDownTimer(timeint * 60 * 1000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {          
        Log.i(TAG, millisUntilFinished " millis left");
    }

    @Override
    public void onFinish() {
        Log.i(TAG, "Timer finished");
    }
};

cdt.start();

Answer:

You can use Timer and for that implementation you can done like

new java.util.Timer().schedule( 
    new java.util.TimerTask() {
        @Override
        public void run() {
            // your code here
        }
    }, 
    5000 
);

Answer:

You Can use Alarm manger If u want to do work even application is inactive(Close)

it keep run until u don’t stop it.

here is full Example

url

http://javatechig.com/android/repeat-alarm-example-in-android

Answer:

Declare a Handler object:

Handler myHandler = new Handler();
myHandler.postDelayed(new Runnable() {
   @Override
   public void run() {
       //Do something after 20 seconds
    //call the method which is schedule to call after 20 sec
   }
}, 200000);  //the time is in miliseconds

-again for 10 seconds do as the same as above and make loop for repetition.

Hope this will be helpful …thanks

Answer:

Please check the below code.

new Timer().scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
 // Enter your code
}
            }, 0, 20000);//put here time 1000 milliseconds=1 second

Hope this code will help you.

Answer:

You need to have Handler called every 20 seconds and update the UI during every pass.

Code should be like this:

final Handler handler = new Handler();//class variable
int count = 0;

handler.post(new Runnable() {

  @Override
  public void run() {

    updateCounter(count++);

    if(count < (10*20000)) { 

        handler.postDelayed(this, 20000);

    }

  }

});

And function to update counter:

private void updateCounter(final int count) {
  runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // you have the seconds passed
        // do what ever you want
    }
  });
}

Answer:

You need to use a custom Handler to do the job. Here is an example of what you need:

public class Main extends Activity  { 
  private TextView txtStatus;
  private RefreshHandler mRedrawHandler = new RefreshHandler();

  class RefreshHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
      Main.this.updateUI();
    }

    public void sleep(long delayMillis) {
      this.removeMessages(0);
      sendMessageDelayed(obtainMessage(0), delayMillis);
    }
  };

  private void updateUI(){
    int currentInt = Integer.parseInt((String) txtStatus.getText()) + 10;
    if(currentInt <= 100){
      mRedrawHandler.sleep(10000);
      txtStatus.setText(String.valueOf(currentInt));
    }
  }

  @Override 
  public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main);
    this.txtStatus = (TextView) this.findViewById(R.id.txtStatus);

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
    @Override
      public void run() {
          updateUI();
        }
    }, 20000);  

  } 
}

Answer:

Use timer class

private  CountDownTimer countDownTimer;

private void startTimer(final Context context) {

    if (countDownTimer != null) {
        countDownTimer.cancel();
        countDownTimer = null;
    }

    countDownTimer = new CountDownTimer(20 * 1000, 1 * 1000) {

        public void onTick(long millisUntilFinished) {

            //
        }

        public void onFinish() {



           yourMethod();



                startTimer(context);

            }
        }

    }.start();
}