Confused about how to solve thread, logoff, handler, and message

Source: Internet
Author: User

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 broadcast we created are all handled by a main thread. Here we can understand it as a UI thread. however, it takes a long time to perform some time-consuming operations, such as reading and writing large files, database operations, and network downloads. In order not to block the user interface, the ANR response Prompt window appears, at this time, we can consider using the thread to solve the problem.

What problems do Android users encounter when using the thread?

For programmers who have been engaged in the development of J2EE, thread is relatively simple. They can directly create and rewrite the run method anonymously and call the start method for execution. it can be inherited from the runnable interface, but for the Android platform, the UI control is not designed as a thread security type. Therefore, some synchronization mechanisms need to be introduced to refresh it, in this case, Google designed Android by referring to the Win32 message processing mechanism.

1. you can use the postinvalidate () method in the thread to refresh a view as the base class interface, and provide some rewriting methods such as postinvalidate (INT left, int top, int right, int bottom) to refresh a rectangular area and perform delayed operations, such as postinvalidatedelayed (long delaymilliseconds) or postinvalidatedelayed (long delaymilliseconds, int left, int top, int right, int bottom) method. The first parameter is millisecond, as shown below:

2. of course, the recommended method is to use a handler to handle this. You can call the postmessage or sendmessage method of the handler object in the run method of a thread. The Android Program maintains a message queue, this will be handled in turn. If you are a Win32 programmer, you can understand the message processing well. However, compared with Android, pretranslatemessage does not provide internal interference methods.

3. What is logoff? In fact, every thread in Android is followed by a logoff. logoff can help the thread maintain a message queue. The problem of yesterday was mentioned in the article "can't create handler inside thread error, but logoff has nothing to do with handler. We can see from the open source code that android also provides a thread inheritance class handerthread to help us deal with it, in the handlerthread object, you can use the getlooper method to obtain a looper object control handle. We can map this Looper object to a handler to implement a thread synchronization mechanism, the logoff object needs to be initialized for execution. the prepare method is the problem we saw yesterday. At the same time, we need to release resources and use logoff. release Method.

4. What is message on Android? For Android, handler can pass some content, and the bundle object can encapsulate string, integer, and blob binary objects, we use the sendemptymessage or sendmessage method of the handler object in the thread to transmit a bundle object to the handler processor. the handler class provides the override method handlemessage (Message MSG) to determine, through MSG. what is used to differentiate each piece of information. unpackage bundle to update the content in the UI thread of the handler class to refresh the control. related handler objects related to sendxxxx message sending methods, as well as postxxxx related methods, which are basically the same as those in Win32. One is to directly return after sending, and the other is to return after processing:

5. java. util. concurrent object analysis: programmers engaged in Java development in the past will not be unfamiliar with the concurrent object. He is an important feature added after JDK 1.5 as a handheld device, and we do not advocate the use of this class, considering that android has designed a task mechanism for us, the android development network does not repeat it too much here. For the relevant reasons, refer to the following introduction:

6. android also provides a processing method different from the thread, namely task and asynctask. From the open source code, we can see that it is a concurrent encapsulation, developers can easily process these asynchronous tasks.

Source: android123

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.