Android Asynctask Instances

Source: Internet
Author: User

. java

1  PackageCom.example.activitydemoay;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.AsyncTask;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.View;8 ImportAndroid.widget.Button;9 ImportAndroid.widget.ProgressBar;Ten ImportAndroid.widget.TextView; One  A  Public classMainactivityextendsActivity { -  - Button Download; the ProgressBar PB; - TextView TV; -  - @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); A  atPB =(ProgressBar) Findviewbyid (R.ID.PB); -TV =(TextView) Findviewbyid (r.id.tv); -  -Download =(Button) Findviewbyid (r.id.download); -Download.setonclicklistener (NewView.onclicklistener () { - @Override in              Public voidOnClick (View v) { -Downloadtask Dtask =NewDownloadtask (); toDtask.execute (100); +             } -         }); the     } *  $     classDownloadtaskextendsAsynctask<integer, Integer, string> {Panax Notoginseng         //The following angle brackets are parameters (in the example, thread break time), progress (publishprogress used), return value type -  the @Override +         protected voidOnPreExecute () { A             //The first method of execution the             Super. OnPreExecute (); +         } -  $ @Override $         protectedString doinbackground (Integer ... params) { -             //second Execution method, OnPreExecute () executes after execution -              for(inti = 0; I <= 100; i++) { the pb.setprogress (i); - publishprogress (i);Wuyi                 Try { theThread.Sleep (params[0]); -}Catch(interruptedexception e) { Wu e.printstacktrace (); -                 } About             } $             return"Execution Complete"; -         } -  - @Override A         protected voidOnprogressupdate (Integer ... progress) { +             //This function is triggered when Doinbackground calls Publishprogress, although only one parameter is called the             //But here is an array, so use progesss[0] to take the value -             //The nth parameter is used Progress[n] to take a value $Tv.settext (Progress[0] + "%"); the             Super. Onprogressupdate (progress); the         } the  the @Override -         protected voidOnPostExecute (String result) { in             //Doinbackground is triggered when the return is returned, in other words, doinbackground after execution is completed. the             //The result here is the return value after the execution of the above doinbackground, so here is "execution complete" the settitle (result); AboutIntent Intent =NewIntent (); the             //Intent passing Parameters the //Intent.putextra ("TEST", "123"); the //Intent.setclass (Mainactivity.this, mainactivityb.class); + //MainActivity.this.startActivity (intent); -             Super. OnPostExecute (result); the         }Bayi  the     } the  -}

. xml

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3android:orientation= "Vertical"4Android:layout_width= "Fill_parent"5android:layout_height= "Fill_parent"6>7<TextView8Android:layout_width= "Fill_parent"9android:layout_height= "Wrap_content"TenAndroid:text= "Hello, Welcome to Andy ' s blog!" /> One<Button AAndroid:id= "@+id/download" -Android:layout_width= "Fill_parent" -android:layout_height= "Wrap_content" theandroid:text= "Download"/> -<TextView -Android:id= "@+id/tv" -Android:layout_width= "Fill_parent" +android:layout_height= "Wrap_content" -Android:text= "Current progress display"/> +<ProgressBar AAndroid:id= "@+id/pb" atAndroid:layout_width= "Fill_parent" -android:layout_height= "Wrap_content" -Style= "? Android:attr/progressbarstylehorizontal"/> -</LinearLayout>

Android Asynctask Instances

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.