In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what are the common problems in Android development. The introduction in the article is very detailed and has certain reference value. Interested friends must read it!
1) What is the difference between thread and service?
(2) What is the difference between new Handler() in an Activity and new Handler() in a Thread you create?
The first question is actually a pseudo-command, because Service is one of the four components of Android, and Thread is just a tool class provided by Java that encapsulates thread management. Whether it is Activity or Service, you can create a worker thread through Thread, but many novices will not understand the difference between them.
The second question involves the knowledge point that Android development must master: Handler. This article starts with this question and talks about my understanding of Handler.
When an Android app starts, a main thread is created by default. After the main thread starts, it first completes the UI drawing, and then enters a message loop, waiting and executing various messages and events from the system, various user click/touch events, message events sent by other threads, and so on. This is a common pattern of thread work, which goes into a loop of "wait for command"->"execute command/message"->"wait for command/message."
So how do other non-UI threads interact with the main thread that enters the message loop? That depends on the Handler.
Handler is an interface provided by Android system for worker thread to interact with the outside world. Through sendMessage() method provided by Handler, the outside world can send various message events to worker thread. Handler completes the binding to the specified thread through a constructor defined as follows:
public Handler() { this(null, false);}public Handler(Looper looper) { this(looper, null, false);}public Handler(Looper looper, Callback callback) { this(looper, callback, false);}public interface Callback { public boolean handleMessage(Message msg);}
Looper is the object responsible for implementing message loop inside thread. There is no such message loop object inside ordinary Java.Thread thread. Android specially provides HandlerThread encapsulation, a thread with message loop mechanism. Handler completes the binding to the Thread by binding to the Thread's Looper object.
Callback is the callback interface for sending out received messages from within the worker thread. After other threads send messages to the worker thread through sendMessage of Handler, the worker thread will notify the listener of the received message through Callback.
Note: By default, if no Looper object (or null) is passed to a thread when new Handler(), the system will bind to the thread that created the Handler() object by default.
So, now it's time to answer the second question, what's the difference between new Handler() in an Activity and new Handler() in a Thread you create?
Answer:
Activy works in the main thread by default, so after new Handler() in the Activity, the Handler object is bound to the Looper object of the main thread by default, so the Handler.sendMessage message is sent to the main thread, and the handleMessage() callback obtained by passing the Callback object also works in the main thread, which is why you can update the UI in the Activity without causing ANR by using the following method:
new Handler( new Handler.Callback() { @Override public boolean handleMessage(Message msg) { UpdateUI(); return false; }});
Similarly, if new Handler() is in a custom thread, then by default the Handler() is bound to the Looper object of the thread, so the Handler.sendMessage message is sent to this thread, and the handleMessage() callback obtained by passing the Callback object is also working in this thread. Therefore, in this case, UI update operations cannot be performed in the handleMessage() function, otherwise ANR will result.
The above is all the content of this article "What are the common problems in Android development", thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.