Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat a task after a fixed amount of time in android?

Tags:

java

android

I want to repeatedly call a method after every 5-seconds and whenever I wish to to stop the repeated call of the method I may stop or restart the repeated call of the method.

Here is some sample code that whats really I want to implement. Please help me in this respect I would be very thankful to you.

private int m_interval = 5000; // 5 seconds by default, can be changed later private Handler m_handler;  @Override protected void onCreate(Bundle bundle) {   ...   m_handler = new Handler(); }  Runnable m_statusChecker = new Runnable() {      @Override       public void run() {           updateStatus(); //this function can change value of m_interval.           m_handler.postDelayed(m_statusChecker, m_interval);      } };  public void startRepeatingTask() {     m_statusChecker.run();  }  public void stopRepeatingTask() {     m_handler.removeCallbacks(m_statusChecker); }   
like image 348
user2391890 Avatar asked Aug 21 '13 09:08

user2391890


People also ask

How to repeat task in Android?

It is fairly easy to schedule a repeating task using Handler as you can just embed the next event within the previous one. Handler handler = new Handler(); private Runnable periodicUpdate = new Runnable () { @override public void run() { // scheduled another events to be in 10 seconds later handler.

How do you call a function after an android time?

You can use this for Simplest Solution: new Handler(). postDelayed(new Runnable() { @Override public void run() { //Write your code here } }, 5000); //Timer is in ms here.

What is a handler in Android Studio?

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler it is bound to a Looper .

How to execute async task repeatedly after fixed time intervals in Android?

How to execute Async task repeatedly after fixed time intervals in Android? AndroidApps/ApplicationsMobile Development This example demonstrates how do I execute AsyncTask repeatedly after fixed time intervals in android. Step 1− Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How do I repeat periodic tasks within an application?

Repeating periodic tasks within an application is a common requirement. This functionality can be used for polling new data from the network, running manual animations, or simply updating the UI. There are at least four ways to run periodic tasks: Handler - Execute a Runnable task on the UIThread after an optional delay.

How do I schedule a task to run every 5 seconds?

In onCreate scheduleTaskExecutor = Executors.newScheduledThreadPool(5); //Schedule a task to run every 5 seconds (or however long you want) scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { @Override public void run() { // Do stuff here!

Can a recurring task start a service?

A recurring task can starta service, which is another matter entirely. BroadcastReciever with AlarmManager For longer sleep intervals (>15 minutes), this is the way to go. AlarmManageralready has constants ( AlarmManager.INTERVAL_DAY) suggesting that it can trigger tasks several days after it has initially been scheduled.


2 Answers

Set repeated task using this:

//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)     }  }, //Set how long before to start calling the TimerTask (in milliseconds) 0, //Set the amount of time between each execution (in milliseconds) 1000); 

and if you wanted to cancel the task simply call t.cancel() here t is your Timer object

and you can also check comment placed below your answer they have given brief information about that.

like image 84
Gru Avatar answered Sep 29 '22 03:09

Gru


Use a Handler in the onCreate() method. Its postDelayed() method causes the Runnable to be added to the message queue and to be run after the specified amount of time elapses (that is 0 in given example). Then this will queue itself after fixed rate of time (1000 milliseconds in this example).

Refer this code :

public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);      android.os.Handler customHandler = new android.os.Handler();     customHandler.postDelayed(updateTimerThread, 0); }  private Runnable updateTimerThread = new Runnable() {     public void run()     {         //write here whaterver you want to repeat         customHandler.postDelayed(this, 1000);     } }; 
like image 44
Gazal Patel Avatar answered Sep 29 '22 03:09

Gazal Patel