AsyncTask example

 

 

package com.example.progress;

import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
    Button download;
    ProgressBar pb;
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pb = (ProgressBar) findViewById(R.id.pb);
        tv = (TextView) findViewById(R.id.tv);

        final Integer[] time = new Integer[100];
        for (int i = 0; i < 100; i++) {
            time[i] = (i + 1) * 10 >> 1;
        }

        download = (Button) findViewById(R.id.download);
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownloadTask dTask = new DownloadTask();
                dTask.execute(time);
                //dTask.execute(100);//如果只传一个参数,doInBackground()中通过 params[0]来访问这个参数
} }); }
class DownloadTask extends AsyncTask<Integer, Integer, String> { // 后面尖括号内分别是参数(例子里是线程休息时间),进度(publishProgress用到),返回值 类型 private static final String TAG = "CM-DownloadTask"; public DownloadTask() { Log.d(TAG, "DownloadTask() thread id: " + Thread.currentThread().getId()); } @Override protected void onPreExecute() { Log.d(TAG, "onPreExecute()"); } @Override protected String doInBackground(Integer... params) { Log.d(TAG, "doInBackground() thread id: " + Thread.currentThread().getId()); for (int i = 0; i < 100; i++) { publishProgress(i + 1); //这个函数是对UI操作,但是是特殊处理了的,所以可以放这个线程里面做 //必须至少取到100,否则进度条不满。甚至可以大于100(但不推荐这么做) pb.setProgress(i + 1); try { Log.d(TAG, "doInBackground:" + params[i]); Thread.sleep(params[i]); } catch (InterruptedException e) { e.printStackTrace(); } } return "执行完毕"; } @Override protected void onProgressUpdate(Integer... progress) { Log.d(TAG, "onProgressUpdate() thread id: " + Thread.currentThread().getId()); // 这个函数在doInBackground调用publishProgress时触发,虽然调用时只有一个参数 // 但是这里取到的是一个数组,所以要用progesss[0]来取值 // 第n个参数就用progress[n]来取值 tv.setText(progress[0] + "%"); } @Override protected void onPostExecute(String result) { Log.d(TAG, "onPostExecute() thread id: " + Thread.currentThread().getId()); // doInBackground返回时触发,换句话说,就是doInBackground执行完后触发 // 这里的result就是上面doInBackground执行后的返回值,所以这里是"执行完毕" setTitle(result); } } }

 

<?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:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello , Welcome to Andy&apos;s Blog!" />

    <Button
        android:id="@+id/download"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Download" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="当前进度显示" />

    <ProgressBar
        android:id="@+id/pb"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

 

posted @ 2015-04-01 23:52  牧 天  阅读(134)  评论(0)    收藏  举报