Android-ui Threads and Asynchronous (Asynctask) threads

Source: Internet
Author: User

UI Threads and Asynchronous (asynctask) Threads


This address: Http://blog.csdn.net/caroline_wendy


the Android UI thread , also known as single-threaded , is a single-threaded application that runs all commands serially, and each call is blocked. ANR (Application Not responding) dialog box , the Android system will prompt you to terminate the application when it is not responding for a certain amount of time (usually 5 seconds) . The main UI runs in the foreground, and the extra UI (such as networking) runs in the background; You can use the Asynctask class to perform multithreading. The Asynctask class has three main operations: Doinbackground () operations performed in the background,onprogressupdate () action at the beginning of the task,OnPostExecute () operation when the task is completed;
How to use: New Posttask (). Execute (status);passing parameters to Doinbackground (), in which operations can be performed;the operation execution of Doinbackground () returns a string type to OnPostExecute ();Note:doinbackground () is a method that runs on a different thread and cannot invoke the UI;OnPostExecute is a method that runs in the main UI and can update the interface.
Code:

@Override public void OnClick (View v) {String status = Meditstatus.gettext (). toString (); LOG.D (TAG, "OnClick with Status:" + status);    Via New Posttask (). Execute (status);  Private Final class Posttask extends Asynctask<string, Void, string> {@Override protected String            Doinbackground (String ... params) {yambaclient yambaclient = new Yambaclient ("Student", "password");                try {yambaclient.poststatus (params[0]);            Return "successfully posted";                } catch (Yambaclientexception e) {e.printstacktrace ();            Return "Failed to post to Yamba service";            }} @Override protected void OnPostExecute (String s) {Super.onpostexecute (s);        Toast.maketext (Statusactivity.this, S, Toast.length_long). Show (); }    }



Android-ui Threads and Asynchronous (Asynctask) threads

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.