Available for download on

Wrox.com private Handler handler = new Handler();

// Initialize a handler on the main thread.

private void mainProcessing() {

Thread thread = new Thread(null, doBackgroundThreadProcessing,



private Runnable doBackgroundThreadProcessing = new Runnable() { public void run() {


// Method which does some processing in the background, private void backgroundThreadProcessing() { [ ... Time consuming operations ... ] handler.post(doUpdateGUI);

// Runnable that executes the update GUI method, private Runnable doUpdateGUI = new Runnable() { public void run() { updateGUI();

}; continues

LISTING 9-13 (continued)

private void updateGUI() {

The Handler class also lets you delay posts or execute them at a specific time, using the postDelayed and postAtTime methods respectively.

Displaying a Toast!

Displaying a Toast!



Toasts are transient Dialog boxes that remain visible for only a few seconds before fading out. Toasts don't steal focus and are non-modal, so they don't interrupt the active application.

Toasts are perfect for informing your users of events without forcing them to open an Activity or read a Notification. They provide an ideal mechanism for alerting users to events occurring in background Services without interrupting foreground applications.

The Toast class includes a static makeText method that creates a standard Toast display window. Pass the application Context, the text message to display, and the length of time to display it (length_short or length_long) in to the makeText method to construct a new Toast. Once a Toast has been created, display it by calling show, as shown in Listing 9-14.


Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment