Android AsyncTask is a lightweight asynchronous task processing class
Common steps->Create an asynchronous task processing class inherited from the AsyncTask class
< img alt="Share a picture" src="/wp-content/uploads/images/mobile/android/1626792373402C:Usersy50003490Downloads" >
(AsyncTask
Params to start the task execution input parameters, such as a set of URLs
Progress background task execution percentage
Result The return result after the task is executed)
< /p>
class MyAsyncTask extends AsyncTask{
@Override
protected String doInBackground(Integer... params) {
//Run on worker thread
//Execute immediately after the onPreExecute method is executed, responsible for execution
// Time-consuming background processing tasks, you can call the publishProgress method to implement
//Update task progress at the time
}
@Override
protected void onPreExecute() {
//Run on UI thread
//Before performing background time-consuming operations, it is usually used for some Initialization operations, such as progress bar display
}
@Override
protected void onProgressUpdate(Integer... values) {
//Run on UI thread
//In the doBackground method, it will be triggered every time the publishProgress method is called The method
}
}
->Create an instance of MyAsyncTask in the UI thread
p>
->Call mAsyncTask.execute() in the UI thread
A complete example
MainActivity
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
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 {
private TextView txttitle;
private ProgressBar pgbar;
private Button btnupdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txttitle = (TextView)findViewById(R.id.txttitle);
pgbar = (ProgressBar)findViewById(R.id.pgbar);
btnupdate = (Button)findViewById(R.id.btnupdate);
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyAsyncTask myTask = new MyAsyncTask(txttitle,pgbar);
myTask.execute(1000);
}
});
}
}
class DelayOperator {
// Delayed operation to simulate downloading
public void delay()
{
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();;
}
}
}
class MyAsyncTask extends AsyncTask
{
private TextView txt;
private ProgressBar pgbar;
public MyAsyncTask(TextView txt,ProgressBar pgbar)
{
super();
this.txt = txt;
this.pgbar = pgbar;
}
//This method does not run in the UI thread, mainly used Asynchronous operation, by calling publishProgress() method
// trigger onProgressUpdate to operate on the UI
@Override
protected String doInBackground(Integer... params) {
DelayOperator dop = new DelayOperator();
int i = 0;
for (i = 10;i <= 100;i+=10 )
{
dop.delay();
publishProgress(i);
}
//i + params[0].intValue() span>
return "";
}
//This method runs in the UI thread and can be used for UI controls Make settings
@Override
protected void onPreExecute() {
txt.setText("Start executing asynchronous thread~");
}
//In the doBackground method, it will be triggered every time the publishProgress method is called this method
// runs in the UI thread and can operate on UI controls
@Override
protected void onProgressUpdate(Integer... values) {
pgbar.setProgress(values[0].intValue());
}
}
Layout code
activity_main
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
tools:context=”.MyActivity”>
<TextView
android:id=”@+id/txttitle”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
<ProgressBar
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@+id/pgbar”
style=”?android:attr/progressBarStyleHorizontal”/> span>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/btnupdate”
android:text=”Update progressBar”/>
LinearLayout>
class MyAsyncTask extends AsyncTask{
@Override
protected String doInBackground(Integer... params) {
//Run on worker thread
//Execute immediately after the onPreExecute method is executed, responsible for execution
// Time-consuming background processing tasks, you can call the publishProgress method to implement
//Update task progress at the time
}
@Override
protected void onPreExecute() {
//Run on UI thread
//Before performing background time-consuming operations, it is usually used for some Initialization operations, such as progress bar display
}
@Override
protected void onProgressUpdate(Integer... values) {
//Run on UI thread
//In the doBackground method, it will be triggered every time the publishProgress method is called The method
}
}
package com.example.myfirstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
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 {
private TextView txttitle;
private ProgressBar pgbar;
private Button btnupdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txttitle = (TextView)findViewById(R.id.txttitle);
pgbar = (ProgressBar)findViewById(R.id.pgbar);
btnupdate = (Button)findViewById(R.id.btnupdate);
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyAsyncTask myTask = new MyAsyncTask(txttitle,pgbar);
myTask.execute(1000);
}
});
}
}
class DelayOperator {
// Delayed operation to simulate downloading
public void delay()
{
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();;
}
}
}
class MyAsyncTask extends AsyncTask
{
private TextView txt;
private ProgressBar pgbar;
public MyAsyncTask(TextView txt,ProgressBar pgbar)
{
super();
this.txt = txt;
this.pgbar = pgbar;
}
//This method does not run in the UI thread, mainly used Asynchronous operation, by calling publishProgress() method
// trigger onProgressUpdate to operate on the UI
@Override
protected String doInBackground(Integer... params) {
DelayOperator dop = new DelayOperator();
int i = 0;
for (i = 10;i <= 100;i+=10 )
{
dop.delay();
publishProgress(i);
}
//i + params[0].intValue() span>
return "";
}
//This method runs in the UI thread and can be used for UI controls Make settings
@Override
protected void onPreExecute() {
txt.setText("Start executing asynchronous thread~");
}
//In the doBackground method, it will be triggered every time the publishProgress method is called this method
// runs in the UI thread and can operate on UI controls
@Override
protected void onProgressUpdate(Integer... values) {
pgbar.setProgress(values[0].intValue());
}
}
<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:orientation="vertical"
tools:context=".MyActivity">
<TextView
android:id="@+id/txttitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/pgbar"
style="?android:attr/progressBarStyleHorizontal"/> span>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnupdate"
android:text="Update progressBar"/>
LinearLayout>