Android Programming Tutorials

(Romina) #1
Sitting in the Background

At this point, you can rebuild, reinstall, and run the application. When you


choose the Run Long Task menu item, you will see the progress bar appear


for five seconds, progressively updated as the "work" gets done:


Figure 19. The progress bar in action

Extra Credit.................................................................................................


Here are some things you can try beyond those step-by-step instructions:



  • Have the background thread also update some UI element when the


work is completed, beyond dismissing the progress bar. Make sure
you arrange to update the UI on the UI thread!


  • Instead of using Activity#runOnUiThread(), try using a Handler for
    communication between the background thread and the UI thread.

  • Instead of starting a Thread from the menu choice, have the Thread


be created in onCreate() and have it monitor a LinkedBlockingQueue
(from java.util.concurrent) as a source of work to be done. Create a

FakeJob that does what our current long-running method does, and


75
Free download pdf