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