Skip to content
KnowledgeCity

Java for Android: Runnable Tasks and Timers

Learn for mobile functionality with this Intermediate Java for Android Programming course.
Preview the first lesson free — get full access to all 6 lessons.
Course: On-Demand
Intermediate Provider Shad Sluiter  6 Lessons ·  20m  in Arabic, German, English, Spanish, French, Portuguese, Chinese 

Course Description

In this section, you will create an application that demonstrates the use of background tasks. In a multi-tasking environment tasks, or threads, share resources including access to the CPU and memory. In Android, the user interface thread is responsible for listening for click events. It is important not to put a long-term tasks on this thread that will freeze it for more than a few milliseconds. To meet this requirement, Java provides a Runnable process and a Handler class to perform slow operations in the background. This application will demonstrate first how not to program a slow process and then how to create a Runnable task.

What You'll Learn

  • Create an Android application that demonstrates the use of background tasks
  • Demonstrate the dangers of running a CPU-intensive task on the UI thread
  • Create a Runnable process to run work in the background
  • Create an instance of the Handler class to schedule tasks to run on the UI thread
  • Set up a UI and a click listener to launch a background task
  • Create a project in Android Studio to build the background-task application

Key Takeaways

  • In a multi-tasking environment, tasks (threads) share resources including access to the CPU and memory.
  • In Android, the user interface thread is responsible for listening for click events.
  • Long-term tasks should not be placed on the UI thread, as doing so can freeze it for more than a few milliseconds.
  • Java provides a Runnable process and a Handler class to perform slow operations in the background.
  • The application demonstrates first how not to program a slow process and then how to create a Runnable task.

Frequently Asked Questions

What will I build in this course?

You will create an Android application that demonstrates the use of background tasks, first showing how not to program a slow process and then how to create a Runnable task.

Why shouldn't long-running tasks run on the UI thread?

In Android, the user interface thread is responsible for listening for click events, and putting a long-term task on it can freeze the thread for more than a few milliseconds, so the course demonstrates the dangers of running a CPU-intensive task on the UI thread.

What Java features does this course use to handle background work?

It uses Java's Runnable process to run slow operations in the background and the Handler class to schedule tasks to run on the UI thread.

What skills will I gain from this course?

You will gain skills in Android Applications, Android Development, Android UI, AsyncTask, Background Task, and Multithreading.

What topics do the lessons cover?

Lessons cover Background Threads, setting up the UI for a background task, setting a click listener to launch a background task, creating a project in Android Studio, creating a background thread, and creating a Handler to start a background thread.