AsyncTask的具体用法详情可以看代码注释
一、Activity代码
package git.greetty.com.async; import android.os.AsyncTask; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; /** * description: AsyncTask练习 * * author: Greetty * * date: 2016/12/20 14:49 * * update: 2016/12/20 * * version: v1.0 */ public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "MainActivity"; private TextView tv_content; //下载进度 private Button btn_down; //下载 private Button btn_cancel; //取消下载 private ProgressBar pb_progress; //进度条 private MyAsyncTask asyncTask; private int progress = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_content = (TextView) findViewById(R.id.tv_content); btn_down = (Button) findViewById(R.id.btn_down); btn_cancel = (Button) findViewById(R.id.btn_cancel); pb_progress = (ProgressBar) findViewById(R.id.pb_progress); btn_down.setOnClickListener(this); btn_cancel.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_down: asyncTask = new MyAsyncTask(); asyncTask.execute(); btn_down.setEnabled(false); btn_cancel.setEnabled(true); break; case R.id.btn_cancel: //取消一个正在执行的任务,onCancelled方法将会被调用 asyncTask.cancel(true); break; default: break; } } private class MyAsyncTask extends AsyncTask<Void, Integer, String> { //onPreExecute方法用于在执行后台任务前做一些UI操作 @Override protected void onPreExecute() { tv_content.setText("开始加载"); pb_progress.setVisibility(View.VISIBLE); } //doInBackground方法内i部执行后台任务,不可在此方法内修改UI @Override protected String doInBackground(Void... params) { //此处用while去模拟数据会有错误,当取消下载时,依然会一直执行, // 要执行完了while循环才能执行onCancelled()方法,应用下载 while (progress<20){ progress++; publishProgress((int) ((progress / (float) 20) * 100)); SystemClock.sleep(500); Log.e(TAG, "doInBackground: "+progress); } return "下载完成"; } //onProgressUpdate方法用于更新进度信息 @Override protected void onProgressUpdate(Integer... values) { pb_progress.setProgress(values[0]); tv_content.setText("loading..." + values[0] + "%"); Log.e(TAG, "progress: " + progress); } onPostExecute方法用于在执行完后台任务后更新UI,显示结果 @Override protected void onPostExecute(String s) { tv_content.setText(s); pb_progress.setVisibility(View.INVISIBLE); btn_down.setEnabled(true); btn_cancel.setEnabled(false); Log.e(TAG, "onPostExecute: " ); progress=0; } //onCancelled方法用于在取消执行中的任务时更改UI @Override protected void onCancelled() { tv_content.setText("下载取消"); pb_progress.setProgress(0); pb_progress.setVisibility(View.INVISIBLE); btn_down.setEnabled(true); btn_cancel.setEnabled(false); Log.e(TAG, "onCancelled: " ); } } }二、布局文件代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="git.greetty.com.async.MainActivity"> <ProgressBar android:id="@+id/pb_progress" android:layout_width="70dp" android:layout_height="70dp" android:visibility="invisible" android:layout_centerInParent="true" /> <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_below="@+id/pb_progress" android:padding="5dp" android:text="我显示内容" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:layout_weight="1" android:id="@+id/btn_down" android:layout_width="0dp" android:layout_height="wrap_content" android:text="开始下载" /> <Button android:layout_weight="1" android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:text="取消下载" /> </LinearLayout> </RelativeLayout>