Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - running a method periodically using postDelayed() call

I have a situation in an Android app where I want to start a network activity (sending out some data) which should run every second. I achieve this as follows:

In the onCreate() I have the code:

tv = new TextView(this); tv.postDelayed(sendData, 1000); 

The sendData() function:

   Handler handler = new Handler();    private Runnable sendData=new Runnable(){     public void run(){         try {             //prepare and send the data here..             handler.removeCallbacks(sendData);             handler.postDelayed(sendData, 1000);             }         catch (Exception e) {             e.printStackTrace();         }        } }; 

The problem come in like this: When user presses the back buttons and app comes out (UI disappears) the sendData() function still gets executed which is what I want. Now when user re-starts the app, my onCreate() gets called again and I get sendData() invoked twice a second. It goes on like that. Every time user comes out and starts again, one more sendData() per second happens.

What am I doing wrong? Is it my new Handler() creating problem? What is the best way to handle this? I want one sendData() call per second until user quits the app (form application manager).

like image 996
zolio Avatar asked Jun 01 '12 06:06

zolio


People also ask

What is new handler () postDelayed?

postDelayed(Runnable r, Object token, long delayMillis) Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. final void. removeCallbacks(Runnable r) Remove any pending posts of Runnable r that are in the message queue.

How do you call a function after a specified delay?

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.

How do I cancel postDelayed handler Android?

removecallback and handler = null; to cancel out the handle just to keep the code clean and make sure everything will be removed.

What is Handler in Android example?

A Handler is a threading class defined in the android. os package through which we can send and process Message and Runnable objects associated with a thread's MessageQueue . You start by creating a Handler instance. Then that instance gets associated with a single thread as well as that thread's message queue.


2 Answers

final Handler handler = new Handler();     handler.postDelayed(new Runnable() {       @Override       public void run() {         //Do something after 100ms         Toast.makeText(c, "check", Toast.LENGTH_SHORT).show();           handler.postDelayed(this, 2000);       }     }, 1500); 
like image 53
Saad Asad Avatar answered Sep 21 '22 10:09

Saad Asad


Perhaps involve the activity's life-cycle methods to achieve this:

Handler handler = new Handler();  @Override protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       handler.post(sendData); }  @Override protected void onDestroy() {       super.onDestroy();       handler.removeCallbacks(sendData); }   private final Runnable sendData = new Runnable(){     public void run(){         try {             //prepare and send the data here..               handler.postDelayed(this, 1000);             }         catch (Exception e) {             e.printStackTrace();         }        } }; 

In this approach, if you press back-key on your activity or call finish();, it will also stop the postDelayed callings.

like image 38
waqaslam Avatar answered Sep 23 '22 10:09

waqaslam