object for each UI thread. Each loose object has a message queue MessageQueue. Each time a handler is declared, the main step is to bind the message queue of the current thread. Then, handler can be used to distribute the required operations to the message queue. Generally,
In an Android program, there are often time-consuming operations such as downloads, network access, and so on, which can be time-consuming if placed on the main thread, which could cause an exception called the ANR exception (application not responding) to block the UI thread. This causes the program to become unresponsive. So we'll put some time-consuming operations on sub-threads, but because Android's UI is not thread-safe, it can cause thread-safety problems if multiple threads operate the U
asynchronous Message Processing in Android consists of four parts, message, Handler, MessageQueue, and Looper. 1. MessageA message is an information that is passed between threads, and it can carry a small number of messages inside to exchange data between different threading. (Field has what arg1 arg2 obj)2. HandlerTh
Windows programmers may know that Windows programs are message-driven and have a global message loop system. Android applications are message-driven, and the message loop mechanism should also be provided. Android uses logoff and Handler to implement the
Handler, loiter, Message analysis, handlerloiter
We all know that time-consuming operations should not be executed in the main thread, such as obtaining data from the server and then updating the interface. However, the interface update can only be executed in the main thread. In this case, the thread is usually enabled to obtain server data, and then the data is sent to the main thread through
. Handler is the same message to communicate.The use of handlerWhen using handler, you need to override the Handlemessage method, accept the message from the new thread in Handlemessage, and do the appropriate processing. Messages are passed through a message in a new thread
1. Background
When using the TCP/IP protocol for data transmission, iocp is often used. We often think that if HTTP can use the multi-thread mode for data interaction, it will bring a better user experience to the system/application. The focus of this study is to access the Web to obtain data and hand it over to another thread for processing. For detailed principles of handler, logoff, and message, refer to
Handler IntroductionAndroid Specifies that only the UI thread is allowed to modify the UI components in a ctivity . When the program starts for the first time, it launches a main thread, often referred to as the UI thread. If the newly-started thread wants to change the value of the attribute in the interface component, use the Handler message-passing mechani
Also because of the weekend night to watch TI3 competition, has not found time to write a blog, leading to have been a long time not updated. Ashamed! You'll get back to your schedule and try to make sure you write every week. Here is also the first congratulations from Sweden's Alliance team won the TI3 champion, hope that the Chinese team will be able to rise next year!
Getting started, we all know that the Android UI is thread-safe, and that if you try a UI operation in a child thread, the p
Handler created, must have a looper, the main thread has created itself. Other threads need to be created by themselves, not by default. Create a method1. This method is to create a system-defined handlerthread, which is run in a non-UI thread, has created a good looper, directly use it. Create handler have multiple constructors, see the source codeHandlerthread Threadhandler = new Handlerthread ("1111");Th
Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/38476887, this article from "Zhang Hongyang's Blog"A group has been created recently. Easy to communicate, Group No.:55032675The previous blog describes the android asynchronous message processing mechanism. Assuming you don't understand, you can see: the Android Asynchronous message processing mechanism gives you a deep under
In Android, it is not safe to update UI controls in a non-main thread, and the app will be crash directly at run time, so when we need to update UI controls in a non-main thread, we need to use handler and message to implementDemo, use to a button and a TextView, click the button to change the content of TextView, button click Time to create a new process, the UI control in the process to modify.1 Public c
. myloue (); check the implementation of the myloue () method:
public static Looper myLooper() { return sThreadLocal.get();}// sThreadLocal.get() will return null unless you've called prepare().static final ThreadLocal
It is time to explain threadlocal. threadlocal provides a copy of the variable value for each thread in Java and is a special thread binding mechanism in Java. Here, we only need to remember the following points: 1. The threadlocal value obtained in each thread is independent o
Many new Android or Java developers are still confused about thread, loose, handler, and message, and derived from handlerthread and Java. util. concurrent, task, and asynctask do not discuss these issues due to books and other materials currently on the market. Today, we will make a more systematic summary of this issue.
Why is thread required during Android development?
The Service, activity, and broadcas
Obtainmessage () is used from the message pool. One MSG does not need to be opened up. New
New requires a new application, which is less efficient and can be recycled by obtianmessage;
// Use handler. obtainmessage (), instead of MSG = new message (); // because if there is already an message object, that not be
1, OnCreate () write good handler acceptance mechanism, ready to accept and process the message2, thread in the use of Handler.post (Runnable R): protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.id.text); Handler=NewHandler ();//new Myhandlerthread (
Write Toast.maketext (Getapplicationcontext (), "SD does not exist", toast.length_short) in the Onhandleintent method in your own Intentsevice inheritance class. Show ( ); When there is no prompt, logcat "Sending message to a Handler on a dead thread" error. After the error from the Internet to understand the reason that when a thread's message loop has exited, y
In the Android system, the class handler has the following two functions mainly.Sends a message in a newly-started thread.Gets and processes the message in the main thread.Class handler When you do this, you first send a message in a newly-started thread, and then you get an
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.