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>  
  • 相关阅读:
    Spider爬虫清洗数据(re方法)
    Python 操作 mongodb 数据库
    python操作mysql数据库
    BeautifulSoup高级应用 之 CSS selectors /CSS 选择器
    mongoDB在centos7上的安装
    CentOS7安装mongoDB数据库
    [洛谷P4602] CTSC2018 混合果汁
    [洛谷P2605] ZJOI2016 基站选址
    [CF1039D] You Are Given a Tree
    [CF1105E] Helping Hiaset
  • 原文地址:https://www.cnblogs.com/jenson138/p/4288830.html
Copyright © 2011-2022 走看看