AndroidArticleAndroid开发Android技术知识

使用AsyncTask类

2016-12-07  本文已影响142人  dayang
一、继承AsycnTask

继承AsyncTask类时,AsyncTask<Params,Progress,Result>类的三种泛型参数:

class MyAsyncTask2 extends AsyncTask<Integer,String,Double>{
    @Override    
    protected Double onProgressUpdate(String... values) {
        super.onProgressUpdate(values);    
}
    @Override
    protected Void doInBackground(Integer... integers) {
        publishProgress("呵呵哒");
        return null;
}
    @Override
    protected void onPostExecute(Double dou) {
        super.onPostExecute(dou);
}
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
}}
二、五个需要掌握的方法:
三、简单案例使用AsyncTask类
案例效果图,下载并显示进度
AsyncTask.png
案例xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下载"
        android:onClick="onClick"/>
    <ProgressBar
        android:id="@+id/proBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:max="100"/>
    <TextView
        android:id="@+id/tvAsync"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="17sp"/>
</LinearLayout>
案例java文件
  1. 继承AsyncTask类,定义doInBackground()处理参数,publishProgress()进度参数,以及返回参数,重写五个方法
public class AsyncTaskActivity extends AppCompatActivity {
    TextView textView;
    ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async);
        initView();
    }
    private void initView(){
        textView= (TextView) findViewById(R.id.tvAsync);
        progressBar= (ProgressBar) findViewById(R.id.proBar);
    }

    public void onClick(View view){
        MyAsyncTask task=new MyAsyncTask(); //创建AsyncTask对象
        //在主线程中调用execute()方法
        //execute()方法只能被调用一次,调用多次会报错
        task.execute(0);
    }
    //Integer:初始化的参数:doInBackground
    //Integer:进度的参数:onProgressUpdate()
    //void:doInBackground返回的结果:onPostExecute()参数
    //<Params,Progress,Result>
    class MyAsyncTask extends AsyncTask<Integer,Integer,Void>{
        //这个方法的代码,会在一个单独 的后台线程中被执行
        //因此,它可以做耗时操作,但不能更新UI
        @Override
        protected Void doInBackground(Integer... integers) {
            int i=integers[0];
            while(i<100){
                i++;
                //发布进度,当这个方法被调用的时候
                //onProgressUpdate()方法会在主线程中被调用
                publishProgress(i);
                try {
                    Thread.sleep(80);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        //当publishProgress调用的时候,这个方法在主线程中调用
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            progressBar.setProgress(values[0]);
            textView.setText(values[0]+"%");
        }
        //当耗时任务执行完之后,在主线程中调用,
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Toast.makeText(AsyncTaskActivity.this,"下载完成",Toast.LENGTH_SHORT).show();
        }
        //在耗时任务执行之前,在主线程中调用
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(AsyncTaskActivity.this,"下载开始",Toast.LENGTH_SHORT).show();
        }
    }
    }
上一篇 下一篇

猜你喜欢

热点阅读