What are threads in Android
There are situations when operations from the main thread should be paged out into separate threads. For this, Android basically offers different approaches. This article is about a simple and lightweight implementation based on Handler and Looper walk.
First it comes to the implementation of the worker thread: For this, the implementation is based on a simple Java thread:
In the main thread of the application, the call is made via
In order to exchange messages between threads, it is necessary to give the receiving thread a MessageQueue. This is done using the class Looper. The static methods prepare () takes care of the creation of the message queue. With a call from loop () processing of the message queue is started:
Send and receive messages
In order to be able to receive and send messages, an instance of the type Handler needed. It is important at this point that the generated handler instance is automatically assigned to the generating thread. With implementation of the method handleMessage (Message) individual handling of incoming messages can be implemented.
So that messages can now be sent to this worker thread, the sender only needs a reference to the handler instance of the receiving thread. With a call from sendMessage (Message) a message can then be sent to the associated worker thread:
About Andreas Schrade
IT company owner and enthusiastic Android Developer expert who serve companies in all over the world. Intrigued by ideas, challenging projects, lifelong learning, armchair philosophy, and even better conversations.
Seeking to be inspired, to envision the unlikely, to work hard for things that are worth it, and to be surrounded by those who bring out the best in me.
on Twitter @andreasschrade
on LinkedIn andreasschrade
Commentscomments powered by Disqus
- What is the Tamil word for busy
- Why is Iran promoting its nuclear weapons
- What replaced Microsoft Word in 2019
- Where did NASA start?
- Why are some people against private schools
- What is implicit data
- Is AIDS a sexually transmitted disease?
- A stray kitten can eat eggs
- Canada goose coats are cheaper in Canada
- How does Uber choose the closest driver
- Feel responsible to humanity
- How to learn Vue js
- What does ahimsa actually mean?
- What are some good hacks for algebra
- Why Choose a Digital Marketing Best Career
- Do women love to be taken out
- Did Eol deserve to die?
- Who is the CEO of Grantland
- Is the use of advertising blocks justified?
- Where can I get an essay topic
- What can prevent nostril cautery
- What are not musical sounds
- Which book should you never read twice?
- How many lighthouses are still working