什么是AsyncTask
- AsyncTask是对Handler与线程池的封装。使用它的方便之处在于能够更新用户界面,当然这里更新用户界面的操作还是
在主线程中完成的,但是由于AsyncTask内部包含一个Handler,所以可以发送消息给主线程让它更新UI。另外,AsyncTask内还包含了一个线程池。使用线程池的主要原因是避免不必要的创建及销毁线程的开销。
如何使用AsyncTask
1.execute(Params… params),执行一个异步任务,需要我们在代码中调用此方法,触发异步任务的执行。
2.onPreExecute(),在execute(Params… params)被调用后立即执行,一般用来在执行后台任务前对UI做一些标记。
3.doInBackground(Params… params),在onPreExecute()完成后立即执行,用于执行较为费时的操作,此方法将接收输入参数和返回计算结果。在执行过程中可以调用publishProgress(Progress… values)来更新进度信息。
4.onProgressUpdate(Progress… values),在调用publishProgress(Progress… values)时,此方法被执行,直接将进度信息更新到UI组件上。
5.onPostExecute(Result result),当后台操作结束时,此方法将会被调用,计算结果将做为参数传递到此方法中,直接将结果显示到UI组件上。
使用AsyncTask做进度条倒计时
package com.lenovo.zy.jindutiao;
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.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button daojishiBtn;
private EditText daojishiET;
private TextView daojishiTV;
private int time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
daojishiBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
time= Integer.parseInt(daojishiET.getText().toString());
new MyTask().execute(time);
}
});
}
private void bindID() {
daojishiBtn=findViewById(R.id.daojishi_btn);
daojishiET=findViewById(R.id.daojishi_et);
daojishiTV=findViewById(R.id.daojishi_tv);
}
class MyTask extends AsyncTask<Integer, Integer, String> {
@Override
protected String doInBackground(Integer... integers) {
for (int i=integers[0];i>0;i--){
try{
Thread.sleep(1000);
publishProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "计时结束";
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
daojishiTV.setText(values[0]+"");
}
@Override
protected void onPostExecute(String r) {
super.onPostExecute(r);
daojishiTV.setText(r);
}
}
}
我更喜欢Handler
1.hander的代码比AsyncTask更容易理解,但handler的代码长度比AsyncTask更长,所以handler相对更为复杂!
如下代码所示:
Handler
package com.lenovo.zy.daojishi;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button daojishiBtn;
private EditText daojishiET;
private TextView daojishiTV;
private int time;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
daojishiTV.setText(msg.what+"b+" +
".");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
daojishiBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
time= Integer.parseInt(daojishiET.getText().toString());
new Thread(new Runnable() {
@Override
public void run() {
for (int i=time;i>0;i--){
handler.sendEmptyMessage(i);
// Message message=handler.obtainMessage();
// message.what=time;
// handler.sendMessage(message);
try{
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
private void bindID() {
daojishiBtn=findViewById(R.id.daojishi_btn);
daojishiET=findViewById(R.id.daojishi_et);
daojishiTV=findViewById(R.id.daojishi_tv);
}
}
AsyncTask
package com.lenovo.zy.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.TextView;
public class MainActivity extends AppCompatActivity {
private TextView downInfoTV;
private Button downBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
downBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new MyTask().execute();
}
});
}
private void bindID() {
downBtn=findViewById(R.id.down_btn);
downInfoTV=findViewById(R.id.down_info);
}
class MyTask extends AsyncTask<Integer,Integer,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
downInfoTV.setText("正在下载");
}
@Override
protected String doInBackground(Integer... integers) {
for (int i=0;i<5;i++){
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "下载完成";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
}