[Android基础]AsyncTask类

效果图:

[Android基础]AsyncTask类_第1张图片

[Android基础]AsyncTask类_第2张图片


package example.com.testui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;


public class AsyncTaskDemo extends Activity {

    private ProgressDialog progressDialog;

    private ListView listView;
    private ArrayList<String> arrayList;
    private ArrayAdapter<String> arrayAdapter;

    private static final int STATE_FINISH = 1;
    private static final int STATE_ERROR = 0;
    private static final int CAPACITY = 50000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_task_demo);

        listView = (ListView)findViewById(R.id.listView);
        ((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                arrayList = new ArrayList<String>();
                new ProgressTask().execute(arrayList);//将参数传到doInBackground
            }
        });
    }

    //参数的类型,进度的类型,返回结果的类型
    //AsyncTask必须在UI线程中实例化它,并且只能执行一次
    private class ProgressTask extends AsyncTask<ArrayList<String>,Integer,Integer>{

        //在主线程执行,任务执行前调用
        @Override
        protected void onPreExecute() {
            progressDialog = new ProgressDialog(AsyncTaskDemo.this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setTitle("提示");
            progressDialog.setMessage("数据加载中...");
            progressDialog.setMax(CAPACITY);
            progressDialog.setIndeterminate(false);
            progressDialog.show();
        }

        //在后台线程执行,把耗时的东西放在这里执行
        @Override
        protected Integer doInBackground(ArrayList<String>... params) {
            //ArrayList<String> data = params[0];
            for(int i = 0;i < CAPACITY;i++) {
                arrayList.add("数据" + i);
                publishProgress(i);//调用onProgressUpdate
            }
            return STATE_FINISH;
        }

        //在主线程执行,用于显示任务执行的速度
        @Override
        protected void onProgressUpdate(Integer... values) {
            progressDialog.setProgress(values[0]);
        }

        //在主线程执行,执行的结果作为方法的参数返回
        @Override
        protected void onPostExecute(Integer integer) {
            int state = integer.intValue();
            progressDialog.cancel();
            arrayAdapter = new ArrayAdapter<String>(AsyncTaskDemo.this,R.layout.list_test,arrayList);
            listView.setAdapter(arrayAdapter);
            switch (state){
                case STATE_FINISH:
                    Toast.makeText(getApplicationContext(),"加载完成!",Toast.LENGTH_LONG).show();
                    break;
                case STATE_ERROR:
                    Toast.makeText(getApplicationContext(),"加载失败!",Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }
    }
}


list_test.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
/>

你可能感兴趣的:(AsyncTask)