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 the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.