android AsyncTask的使用

xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="example.com.asynctaskdemo.MainActivity">


    <Button
        android:id="@+id/startdownload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始下载"
        />

    <ProgressBar
        android:id="@+id/probat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"

        />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="20sp"
        />


</LinearLayout>

java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button startdownload;
    private ProgressBar probar;
    private TextView tv;

    private DownTask task;


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


        startdownload= (Button) findViewById(R.id.startdownload);
        probar= (ProgressBar) findViewById(R.id.probat);

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

        startdownload.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        task=new DownTask();
        task.execute();

        Toast.makeText(getApplicationContext(),"才始运行异步应用",Toast.LENGTH_SHORT).show();

    }


    //异步处理
    class DownTask extends AsyncTask<String,Integer,String>{


        @Override //该方法非在主线程工作。可进行耗时操作,不可更新UI界面,其它方法为主线程运行
        protected String doInBackground(String... params) {

            for (int i=1;i<=100;i++){
                try{  //模拟下载操作

                    Thread.sleep(333);
                    publishProgress(i);

                }catch (Exception e){
                    e.printStackTrace();
                }

            }

            String result="任务已完成";

            return result;    //将回调方法执行完毕后,将会调用doInBackground
        }


        //该回调方法执行完毕后,将会调用doInBackground
        @Override
        protected void onPreExecute() {
            probar.setMax(100);
            probar.setProgress(0);
            tv.setText("开始下载");
        }

        @Override    //doInbackground结束后回调该方法,结束
        protected void onPostExecute(String s) {
            Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
            tv.setText("下载完成");

            super.onPostExecute(s);
        }

        @Override     //通知UI界面更新
        protected void onProgressUpdate(Integer... values) {
            probar.setProgress(values[0]);
        }
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_37744986/article/details/81197057
今日推荐