一.AsyncTask是什么?
1.AsyncTask是Android提供的轻量级(实现代码少)的异步类。
2.为了降低异步通信的开发难度,提供了AsyncTask.
3.AsyncTask直接继承Object类,位于android.os包中。
4.使用AsyncTask可以忽略Looper、MessageQueue、Handler等复杂对象,更便捷的完成异步耗时操作。
二.AsyncTask怎么用?
1.新建内部类继承AsyncTask;
2.定义AsyncTask的三种泛型参数;
3.dolnBackground抽象方法;
4.重写onPreExecute方法;
5.重写onProgressUpdate方法;
6.重写onPostExecute方法;
7.在需要启动的地方调用execute方法。
三.需要实现的几个方法分别是什么,有什么用?
1.onPreExecute方法:
(1).异步任务开始执行时,系统最先调用此方法。
(2).此方法运行在主线程中,可以对控件进行初始化等操作。
2.dolnBackground方法:
(1).执行完onPreExecute方法后,系统执行此方法。
(2).此方法运行在子线程中,比较耗时的操作放在此方法中执行。
3.onProgressUpdate方法
(1).显示当前进度,适用于下载或扫描这类需要实时显示进度的需求。
(2).此方法运行在主线程中,可以修改控件状态的方法,例如:显示百分比。
(3).触发此方法,需要在dolnBackground中使用publishProgress方法。
4.publishProgress方法:
(1).在dolnBackground中使用。
(2).用于触发onProgressUpdate方法。
5.onPostExecute方法:
(1).当异步任务执行完成后,系统会调用此方法。
(2).此方法运行在主线程中,可以修改控件状态,例如:下载完成。
四.什么是泛型参数?
AsyncTask有三种泛型参数:
1.Params:启动任务执行的输入参数。
2.Progress:后台任务执行的百分比。
3.Result:后台执行任务最终返回的结果。
五.什么是长度可变的参数列表?
可变长度参数列表,其语法就是类型后跟…,表示此处接受的参数为0到多个Object类型的对象,或者是一个Object[]。
如图:
六.案例解析——倒计时Demo
效果如图
刚运行的效果:
点击开始后:
运行结果:
代码如下:
Activity:
<?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="com.example.androidthree.Download2Activity">
<TextView
android:id="@+id/down_tv2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25dp"
android:gravity="center"/>
<Button
android:id="@+id/down_btn2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="开始"
/>
</LinearLayout>
Java:
package com.example.androidthree;
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.TextView;
public class Download2Activity extends AppCompatActivity {
private TextView down_tv2;
private Button down_btn2;
private int count=10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download2);
down_tv2=findViewById(R.id.down_tv2);
down_btn2=findViewById(R.id.down_btn2);
down_btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DownloadTask().execute();
}
});
}
public class DownloadTask extends AsyncTask<String,String,String>{
//初始化控件
@Override
protected void onPreExecute() {
super.onPreExecute();
down_tv2.setText("正在下载");
}
@Override
protected String doInBackground(String... strings) {
while (count>0){
publishProgress(count+"");
count--;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "下载完成";
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
down_tv2.setText(values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
down_tv2.setText(s);
}
}
}
七.案例解析-进度条Demo
本案例要实现的是:点击第一个按钮,第一个进度条会走;点击第二个进度条时,第二个会走;两个都点击时,都会走。
Activity代码如下:
<?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="com.example.androidthree.AsyncTaskActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp" />
<ProgressBar
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:id="@+id/task_pb"
android:layout_width="match_parent"
android:layout_height="10dp"
android:max="100"
android:progress="0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp" />
<Button
android:id="@+id/task_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="开始1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp" />
<ProgressBar
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:id="@+id/task_pb1"
android:layout_width="match_parent"
android:layout_height="10dp"
android:max="100"
android:progress="0"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp" />
<Button
android:id="@+id/task_btn1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="开始2"/>
</LinearLayout>
效果如图:
Java代码如下:
package com.example.androidthree;
import android.graphics.Color;
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;
public class AsyncTaskActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressBar task_pb, task_pb1;
private Button task_btn, task_btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_async_task);
task_pb = findViewById(R.id.task_pb);
task_btn = findViewById(R.id.task_btn);
task_pb1 = findViewById(R.id.task_pb1);
task_btn1 = findViewById(R.id.task_btn1);
task_btn.setOnClickListener(this);
task_btn1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.task_btn:
task_btn.setEnabled(false);
new DownTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 1);
task_pb.setProgress(0);
break;
case R.id.task_btn1:
task_btn1.setEnabled(false);
new DownTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 2);
task_pb1.setProgress(0);
break;
default:
break;
}
}
public class DownTask extends AsyncTask<Integer, Integer, Integer> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Integer doInBackground(Integer... index) {
for (int i = 0; i < 100; i += 10) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
switch (index[0]) {
case 1:
task_pb.incrementProgressBy(10);
break;
case 2:
task_pb1.incrementProgressBy(10);
break;
default:
break;
}
}
return index[0];
}
@Override
protected void onPostExecute(Integer s) {
super.onPostExecute(s);
switch (s) {
case 1:
task_btn.setText("下载完成");
task_btn.setEnabled(true);
break;
case 2:
task_btn1.setText("下载完成");
task_btn1.setEnabled(true);
break;
default:
break;
}
}
}
}
效果如图: