Simple use of asynctask

Source: Internet
Author: User

1. Layout: Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >        <TextView         android:id="@+id/text"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/>        <ProgressBar        style="?android:attr/progressBarStyleHorizontal"        android:max="100"        android:maxHeight="5dp"        android:id="@+id/probar"        android:layout_width="300dp"        android:layout_height="wrap_content" />    <Button        android:id="@+id/net"        android:onClick="onClick"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/net" />   </LinearLayout>

2. A mainactivity. Java button and a progress bar

Public class mainactivity extends activity {private textview text; private button netbtn; private progressbar probar; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); text = (textview) This. findviewbyid (R. id. text); netbtn = (button) This. findviewbyid (; probar = (progressbar) This. findviewbyid (R. id. probar);} public void onclick (view v) {Switch (v. GETID () {Case progressasynctask task = new progressasynctask (text, probar); // call the background thread task.exe cute (1000); break ;}}}

3. asynctask. Java background Thread class

// When the execute () method is called in the UI thread, onpreexecute () is executed first, doinbackground () is followed, and onpostexecute () is finally executed. // Asynctask generic. The three parameter types can be arbitrary // The first parameter is the parameter type of the doinbackground () method // The second parameter is onprogressupdate () method parameter type // The third parameter is the doinbackground () method return type and onpostexecute () parameter type public class progressasynctask extends asynctask <integer, integer, string> {private textview text; private progressbar probar; Public progressasynctask (textview text, progressbar probar) {super (); this. TEXT = text; this. probar = probar;} // run in the background thread // variable length parameter, Params is a number Group. @ Overrideprotected string doinbackground (integer... params) {netdemo net = new netdemo (); int I = 0; for (I = 10; I <= 100; I ++ = 10) {net. operate (); // publishprogress () calls back the onprogressupdate () method. Publishprogress (I);} return I + Params [0]. intvalue () + "";} // run @ overrideprotected void onpreexecute () {text in the UI thread. settext ("START Asynchronous Operation download");} // run in the UI thread // result is the return value of the doinbackground () method @ overrideprotected void onpostexecute (string result) {text. settext ("Download ended" + result);} // run @ overrideprotected void onprogressupdate (integer... values) {probar. setprogress (Values [0]) ;}}

4. netdemo. Java imitates Network Operations

public class NetDemo {public void operate(){try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


5. Result:


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: 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.