Android Asynchronous Operations Summary

Source: Internet
Author: User

Android often has some operations such as network requests, file read and write, database operations, time-consuming, we need to put it on a non-UI thread to handle, at this time, we need to handle the UI changes and interaction before and after the task. We need to through the similar JS in the asynchronous request processing, here summarizes what I know, to facilitate their own memory, but also convenient for others to browse.

    1. Asynctask

New Aysnctask (). Execute ();

Asynctask will follow the process in the UI thread and a time consuming task thread.

(1) OnPreExecute () performs preprocessing, which runs on the UI thread and can do some preparatory work for the background task, such as drawing a progress bar control.

(2) Doinbackground (params ...) the specific calculations performed by the background process are implemented here, Doinbackground (params ...) is the key to Asynctask, this method must be overloaded. Publishprogress (Progress ...) can be used within this method. Changes the current progress value.

(3) onprogressupdate (Progress ...) runs on the UI thread. If Publishprogress (Progress ...) is used in Doinbackground (Params ...), this method is triggered. Here you can make a specific response to the progress bar control based on the progress value.

(4) OnPostExecute (result) runs on the UI thread and can handle the results of a background task, and the result is Doinbackground (Params ...). 's return value. This method is also frequently overloaded if result null indicates that the background task is not completed (canceled or an exception occurred).

2. Handler

You need to pass lopper when creating handler, which is the UI thread by default.

Send messages (message) to the thread of the main thread or handler via handler.

3. Activity.runonuithread (Runnable)

Runnable can be executed on the UI thread

4. View.post (Runnable)

Runnable running on the UI thread

View.post (Runnable) method. In the post (Runnable action) method, view obtains the handler of the current thread (that is, the UI thread) and then posts the action object into the handler. In handler, it wraps the action object passed in as a message (the callback of the message is the action) and then puts it into the message loop of the UI thread. When handler processes the message again, there is a branch (the one that is not explained) that is set for it to call the runnable run method directly. At this point, it has been routed to the UI thread, so we can update the UI with no worries.

All of the principles of asynchronous operation are essentially through handler

Basically, in these ways, you can also use the message loop as a common task-handling mechanism.

CSDN Blog Editor is not good, also does not support markdown, this is from my personal blog moved over, welcome to individual Independent Blog!

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.