zoukankan      html  css  js  c++  java
  • android AsyncTask实例

    .java

     1 package com.example.activitydemoay;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.AsyncTask;
     6 import android.os.Bundle;
     7 import android.view.View;
     8 import android.widget.Button;
     9 import android.widget.ProgressBar;
    10 import android.widget.TextView;
    11 
    12 public class MainActivity extends Activity {
    13 
    14     Button download;
    15     ProgressBar pb;
    16     TextView tv;
    17 
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_main);
    22 
    23         pb = (ProgressBar) findViewById(R.id.pb);
    24         tv = (TextView) findViewById(R.id.tv);
    25 
    26         download = (Button) findViewById(R.id.download);
    27         download.setOnClickListener(new View.OnClickListener() {
    28             @Override
    29             public void onClick(View v) {
    30                 DownloadTask dTask = new DownloadTask();
    31                 dTask.execute(100);
    32             }
    33         });
    34     }
    35 
    36     class DownloadTask extends AsyncTask<Integer, Integer, String> {
    37         // 后面尖括号内分别是参数(例子里是线程休息时间),进度(publishProgress用到),返回值 类型
    38 
    39         @Override
    40         protected void onPreExecute() {
    41             // 第一个执行方法
    42             super.onPreExecute();
    43         }
    44 
    45         @Override
    46         protected String doInBackground(Integer... params) {
    47             // 第二个执行方法,onPreExecute()执行完后执行
    48             for (int i = 0; i <= 100; i++) {
    49                 pb.setProgress(i);
    50                 publishProgress(i);
    51                 try {
    52                     Thread.sleep(params[0]);
    53                 } catch (InterruptedException e) {
    54                     e.printStackTrace();
    55                 }
    56             }
    57             return "执行完毕";
    58         }
    59 
    60         @Override
    61         protected void onProgressUpdate(Integer... progress) {
    62             // 这个函数在doInBackground调用publishProgress时触发,虽然调用时只有一个参数
    63             // 但是这里取到的是一个数组,所以要用progesss[0]来取值
    64             // 第n个参数就用progress[n]来取值
    65             tv.setText(progress[0] + "%");
    66             super.onProgressUpdate(progress);
    67         }
    68 
    69         @Override
    70         protected void onPostExecute(String result) {
    71             // doInBackground返回时触发,换句话说,就是doInBackground执行完后触发
    72             // 这里的result就是上面doInBackground执行后的返回值,所以这里是"执行完毕"
    73             setTitle(result);
    74             Intent intent = new Intent();
    75             // Intent传递参数
    76 //            intent.putExtra("TEST", "123");
    77 //            intent.setClass(MainActivity.this, MainActivityB.class);
    78 //            MainActivity.this.startActivity(intent);
    79             super.onPostExecute(result);
    80         }
    81 
    82     }
    83 
    84 }

    .xml

     1 <?xml version="1.0" encoding="utf-8"?>  
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     3     android:orientation="vertical"  
     4     android:layout_width="fill_parent"  
     5     android:layout_height="fill_parent"  
     6     >  
     7     <TextView    
     8        android:layout_width="fill_parent"   
     9        android:layout_height="wrap_content"   
    10        android:text="Hello , Welcome to Andy's Blog!"/>  
    11     <Button  
    12        android:id="@+id/download"  
    13        android:layout_width="fill_parent"  
    14        android:layout_height="wrap_content"  
    15        android:text="Download"/>  
    16     <TextView    
    17        android:id="@+id/tv"  
    18        android:layout_width="fill_parent"   
    19        android:layout_height="wrap_content"   
    20        android:text="当前进度显示"/>  
    21     <ProgressBar  
    22        android:id="@+id/pb"  
    23        android:layout_width="fill_parent"  
    24        android:layout_height="wrap_content"  
    25        style="?android:attr/progressBarStyleHorizontal"/>  
    26 </LinearLayout>  
  • 相关阅读:
    浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)
    浙大数据结构课后习题 练习二 7-2 一元多项式的乘法与加法运算 (20 分)
    浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)
    浙大数据结构课后习题 练习一 7-1 最大子列和问题 (20 分)
    PAT Basic 1019 数字黑洞 (20 分)
    PAT Basic 1017 A除以B (20 分)
    PAT Basic 1013 数素数 (20 分)
    PAT Basic 1007 素数对猜想 (20 分)
    PAT Basic 1003 我要通过! (20 分)
    自动化运维——HelloWorld(一)
  • 原文地址:https://www.cnblogs.com/jenson138/p/4288830.html
Copyright © 2011-2022 走看看