Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

1. Threading and Asynchronous Processing > Working with the Thread Class

Working with the Thread Class

If you need to control a thread yourself, use the Thread class (java.lang.Thread). Porting existing code might be simpler using the Thread class directly, instead of AsyncTask. The Activity class that owns the thread is responsible for managing the lifecycle of the thread. Generally speaking, the Activity includes a member variable of type Handler. Then, when the Thread is instantiated and started, the post() method of the Handler is used to communicate with the main UI thread. You can also communicate to the main UI thread using the runOnUiThread() method of the Activity class and the post() and postDelayed() methods of the View class. For example, here is a simple Activity class that performs a similar operation to the AsyncTask example shown earlier in this chapter.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial