Android process and thread details 3: asynctask

Source: Internet
Author: User

Asynctask allows you to execute asynchronous work on your interface. It executes time-consuming operations in a work thread and sends the results to the interface thread. You do not need to manage the thread and handle yourself.


To use it, you must derive a class from asynctask and implement the callback method doinbackground (). This method runs in a background thread pool. To update your interface, you should implement onpostexecute (), which gets the result of doinbackground () and runs it in the interface thread, so you can safely update your interface. You can call execute () in the interface thread to execute the asynctask task.


For example, you can use asynctask to implement the example in the previous chapter:

Public void onclick (view v) {<br/> New downloadimagetask(cmd.exe cute ("http://example.com/image.png"); <br/>}< br/><PRE name = "code" class = "Java"> private class downloadimagetask extends asynctask <string, void, bitmap> {/*** the system sends asynctask.exe cute () and call this method in the background thread to complete the work */protected bitmap doinbackground (string... URLs) {return loadimagefromnetwork (URLs [0]);}/** the doinbackground () and call this method in the interface thread to execute the task */protected void onpostexecute (Bitmap result) {mimageview. setimagebitmap (result) ;}</PRE> <p>

 

Now the interface is secure and the code is simplified. This is because the work is divided into parts completed in the work thread and completed in the interface thread.

You should read the asynctask reference to fully understand how to use this class. Here is an overview of how it works:

  • You can use the parameter type to specify the parameter type, progress value, and task final value.

  • Method doinbackground () is automatically executed in the work thread

  • Onpreexecute (), onpostexecute (),
    And onprogressupdate () are both executed in the interface thread

  • The value returned by doinbackground () is passed to onpostexecute ()

  • You can call publishprogress () in doinbackground () at any time to execute onprogressupdate () in the interface thread ()

  • You can cancel a task in any thread at any time.

Warning another problem you may encounter when using a working thread is the unexpected restart of the activity (for example, the screen direction has changed), which may destroy your working thread. To learn how to prevent your task from being killed when such a phenomenon occurs and how to cancel your task correctly when the activity dies, see the source code of the shelves example.

Related Article

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.