North Border Software




Home Tutorials Hints & Tips About
Share on Facebook Share on Twitter

Getting Started in Android Development

Part 45 - Creating a Thread

In this tutorial we will look at a working example of setting up a thread. A thread in processing terms is a segment of code that is allocated its own processing resources. In the context of an Android app, by default there is one single thread called the main User Interface. If we ran all actions on this thread, then we can cause the app to become unresponsive and ultimately lock up if we are doing intensive processing on that thread. In the example here we will look at updating a clock every second. There is a perpetual loop in the code that has a delay of one second inside the loop. It's easy to imagine what would happen if this loop is executed on the main thread. You can try it and see how the app becomes unresponsive and in fact, you will not see the clock update. What we need to do is update the clock on a new thread and update the clock's TextView on the main User interface. As such the code for the clock update is run separately from the user interface and so will not interfere with it.

A simple layout with a centered TextView to show the clock has been defined. The code for is shown below:

In the Activities java code, we need to define some objects:

TextView currentDateTime;
SimpleDateFormat df;
String formattedDate = null;

The TextView will display the date and time. The date and time in Android devices is stored as an integer value. In fact, that integer value is the number of milliseconds after January 1, 1970 00:00:00.0 UTC. Therefore, we need to set up a dateformater and a string value to show that formatted date and time. We will then set TextView text to the value of the string.

We need to set the date format:

df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Information about over date formats can be found here and it is well worth taking some time to review.

Inside of the onCreate method we need to setup the thread. The full code is shown below:

Thread t = new Thread() {
   @Override
   public void run() {
      while (!IsInterrupted()) {
         try {
            Thread.sleep(1000);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
         runOnUiThread(new Runnable() {
         @Override
         public void run() {
            Calendar c = Calendar.getInstance();
            formattedDate = df.format(c.getTime());
            currentDateTime.setText(formattedDate);
         }
      });
      t.start();
   }
}

We need to override the run method and set up the while loop. The loop will run until it is interrupted by the system. We use the isInterrupted() method to control the loop.

There follows a delay of 1000ms or 1s using the line of code below:

Thread.sleep(1000);

We then need to run the following code on the main User Interface thread by calling the runOnUiThread() method:

Calendar c = Calendar.getInstance();
formattedDate = df.format(c.getTime());
currentDateTime.setText(formattedDate);

We get the current time in milliseconds from 1970 using:

Calendar c = Calendar.getInstance();

We then format the date and time in the way we wish and then set the string value to the formatted date and time.                            

formattedDate = df.format(c.getTime());

Finally, we update the TextView.

currentDateTime.setText(formattedDate);

The loop is repeated. Once the code for the thread is defined we need to start the thread.

t.start();

That's really all there is to it.

After this tutorial your ThreadActivity.java file should look similar to the one below:

Your MainActivity.java file should look similar to the one below:

Your Android Manifest file should look similar to the one below:


Download Download tutorial set 3

© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo

Android Apps and Training