Assembling a Background Thread

Create a new class called ThumbnailDownloader that extends HandlerThread. Then give it a constructor, a stub implementation of a function called queueThumbnail(), and an override of the quit() function that signals when your thread has quit. (Toward the end of the chapter, you will need this bit of information.)

Listing 25.7  Initial thread code (ThumbnailDownloader.kt)

private const val TAG = "ThumbnailDownloader"

class ThumbnailDownloader<in T>
    : HandlerThread(TAG) {

    private var hasQuit = false

    override fun quit(): Boolean {
        hasQuit = true
        return super.quit()

    fun queueThumbnail(target: T, url: String) {
        Log.i(TAG, "Got a URL: $url")

Notice that you gave the class a single generic argument, ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.