Android--Asynchronous Processing tool Class (Asynctask)

Source: Internet
Author: User

The. XML code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <progressbar        android:id= "@+id/bar"        android:layout_width= "Fill_parent"        android:layout_height= "wrap_content"        style= "Android:attr/progressbarstylehorizontal"/>    < TextView        android:id= "@+id/info"        android:layout_width= "fill_parent" android:layout_height= "Wrap_        Content "        android:text=" "/></linearlayout>

. Java code is as follows:

Package Org.lxh.demo;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog;import Android.content.dialoginterface;import Android.os.asynctask;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.progressbar;import Android.widget.textview;public class Hello extends Activity {private ProgressBar bar = null;private TextView info = null;p ublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//life cycle Method Super.setcontentview ( R.layout.main); Set the layout manager to use This.bar = (ProgressBar) Super.findviewbyid (r.id.bar); this.info = (TextView) Super.findviewbyid ( R.id.info); Childupdate child = new Childupdate ();//Sub-Task Object Child.execute (100);//Set sleep time}private class Childupdate extends Asynctask <integer, Integer, string> {//Overwrite the following method @overrideprotected String doinbackground (integer ... params) {//Handle background task for (int x = 0; x < + +) {Hello.this.bar.setProgress (x);//progress bar set this.publishprogress (x);//pass each update content try {Thread.Sleep (params[0]);} catch (Interruptedexception e) {e.printstacktrace ();}} Return "Execution is complete! ";} @Overrideprotected void OnPostExecute (String result) {//The task executes after execution Hello.this.info.setText (result);} @Overrideprotected void Onprogressupdate (Integer ... progress) {//value after each update Hello.this.info.setText ("Current progress is:" + String.valueof (Progress[0]));}}


Android--Asynchronous Processing tool Class (Asynctask)

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.