asynctask

来源:互联网 发布:德里克罗斯数据 编辑:程序博客网 时间:2024/06/02 06:34

package com.example.user.android2lesson_03_asynctask;

import android.os.AsyncTask;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ProgressBar;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    /**

     * AsyncTask异步任务

     * 从字面上说就是在线程之外再异步的完成一些任务

     * 例如:上传,下载等号实行的操作均可用这个来完成

     * AsyncTack就相当于安卓给我们提供了一个多线程的框架,其介于Thread和Handler之间

     * 使用的时候继承AsyncTask,并实现特定的方法即可,无需过多的关心多线程的问题

     *

     

    //步骤一:在布局文件中添加相应的控件

    //步骤二:声明属性并进行初始化

    private Button download;

    private ProgressBar pb;

    private TextView tv;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        download= (Button) findViewById(R.id.download);

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

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

        

        //AsyncTack使用第一步:创建一个内部类,继承自AsyncTask

        

        class DownLoadTask extends AsyncTask<Integer,Integer,String> {

            //上面的三个参数是不固定的可以更改

            //这是因为demo的关系,一个代表休息时间,进度,还有一个返回值类型

            //任务开始时自动执行这个方法

            @Override

            protected void onPreExecute() {

                super.onPreExecute();

            }

            //后台执行的方法

            @Override

            protected String doInBackground(Integer... integers) {

                //模拟下载的进度进行进度的更新

                for (int i=0;i<101

                        ;i++){

                    //调用方法

                    publishProgress(i);

                    //线程休眠一秒,模拟数据的获取

                    try{

                        Thread.sleep(integers[0]);

                    }catch (InterruptedException e){

                        e.printStackTrace();

                    }

                }

                return null;

            }

            //当界面更新的时候自动调用

            @Override

            protected void onProgressUpdate(Integer... values) {

                super.onProgressUpdate(values);

                //这个方法是在别的方法中调用publishProgress时自动被调用的

                //他的参数是一个不定长参数,即参数的个数不一定,相当于一个数组

                //第n个参数就是values[n]

                tv.setText(values[0]+"%");

                //设置进度条的当前进度是多少

                pb.setProgress(values[0]);

            }

            //当执行完成的时候

            @Override

            protected void onPostExecute(String s) {

                super.onPostExecute(s);

            }

        }

        //给download按钮添加响应事件

        download.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                DownLoadTask task=new DownLoadTask();

                task.execute(100);

            }

        });

    }

}

2016/7/6 20:54:42

<?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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:orientation="vertical"

    tools:context="com.example.user.android2lesson_03_asynctask.MainActivity">

    <Button

        android:id="@+id/download"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="下载"

        />

    <TextView

        android:id="@+id/tv"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="当前的进度为"

        android:textSize="14sp"

        />

    <ProgressBar

        android:id="@+id/pb"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

</LinearLayout>

0 0