KIO
Kreative Ideen online
Handlers allow you to schedule code

Handlers allow you to schedule code

In non-Android Java, you can perform tasks like using a background thread. In Android, that approach won’t work – only the main Android thread can update the user interface, and if any other thread tries to do so, you get a CalledFromWrongThreadException.

The solution is to use a Handler.

Handlers allow you to schedule code

A Handler is an Android class you can use to schedule code that should be run at some point in the future. You can also use it to post code that needs to run on a differnent thread than the main Android thread. In our case, we’re going to use a Handler to schedule the stopwatch code to run every second.

To use the Handler, your wrap the code you wish to schedule in a Runnable object, and then use the Handler post() and postDelayed() method to specify when you want the code to run.

The post() method

The post() method posts code that needs to be run as soon as possible (which is usually almost immediately). This method takes one parameter, an object of type Runnable. A Runnable object is a job you want to run. You put the code you want to run in the Runnable’s run() method, and the Handler will make sure the code is run as soon as possible.

Here’s what the method looks like:

final Handler handler = new Handler();
handler.post(Runnable);

The postDelayed() method

The postDelayed() method works in a similar way to the post() method except that you use it to post code that should be run in the future. The postDelayed() method takes two parameters: a Runnable and a long. The Runnable contains the code you want to run in its run() method, and the long specifies the number of millieseconds you wish to delay the code by. The code will run as soon as possible after the delay.

Here’s what the method looks like:

final Handler handler = new Handler();
handler.postDelayed(Runnable, long);

Usage example:

private void runTimer(){
  final TextView timeView = (TextView)findViewById(R.id.time_view);
  //Create a new Handler.  
  final Handler handler = new Handler();
         /*Call the post() method, passing in a new Runnable. The post() method prosesses code
         without a delay, so the code in the Runnable will run almost immediatley*/
         handler.post(new Runnable(){
          @Override

          /*The Runnable run() method contains the code you want to run - in our case, 
             the code to update the text view.*/ 
           public void run(){
                   int hours = seconds/3600;
                   int minutes = (seconds%3600)/60;
                   int secs = seconds%60;
                   String time = String.format(Locale.getDefault(),
                                         "%d:%02:%2d", hours,minutes, secs);
                   timeView.SetText(time);
                   if(running){
                        seconds++;
                   }
                   /*Post the code in the Runnable to be run again after a delay of 1000 milliseconds.
                   As this line of code is included in the Runnable run() method, 
                   it will keep getting called.*/
                   handler.postDelayed(this, 1000);
      }
}};
}      
}

Leave a Reply

Your email address will not be published. Required fields are marked *