Pro Android with Kotlin


CHAPTER 9: User Interface


Download 5.42 Mb.
Pdf ko'rish
bet163/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   159   160   161   162   163   164   165   166   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

193
CHAPTER 9: User Interface
Handlers
A Handler is an object maintaining a message queue for the asynchronous processing of 
messages or Runnable objects. You can use a Handler for asynchronous processes as follows:
var handlerThread : HandlerThread? = null
// or: lateinit var handlerThread : HandlerThread
...
fun doSomething() {
handlerThread = handlerThread ?:
HandlerThread("MyHandlerThread").apply {
start()
}
val handler = Handler(handlerThread?.looper)
handler.post {
// do s.th. in background
}
}
If you create one HandlerThread as in this code snippet, everything that is posted gets run
in the background, but it is executed sequentially there. This means handler.post{}; 
handler.post{} will run the posts in series. You can, however, create more HandlerThread 
objects to handle the posts in parallel. For true parallelism, you’d have to use one 
HandlerThread for each execution.
Loaders
Loaders also do their work in the background. They are primarily used for loading data from 
an external source. Loaders are described in Chapter 
8
.
Supporting Multiple Devices
Device compatibility is an important issue for apps. Whenever you create an app, it is of 
course your goal to address as many device configurations as possible and to make sure 
users who install your app on a certain device can actually use it. Compatibility boils down 
to the following:

Finding a way your app can run with different screen capabilities, including 
pixel width, pixel height, pixel density, color space, and screen shape

Finding a way your app can run with different API versions
Note Handlers were introduced in Android a long time before the new java.util.concurrent 
package was available in Java 7. Nowadays for your own code, you might decide to favor the 
generic Java classes over Handlers without missing anything. Handlers, however, quite often 
show up in Android’s libraries.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   159   160   161   162   163   164   165   166   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling