Android learning (28)AsyncTask? Asynchronous thread

Posted by DavidGS on Thu, 02 Jan 2020 20:56:56 +0100

Android learning (28)AsynTask? Asynchronous thread

AsyncTask is a lightweight asynchronous class provided by android. It can directly inherit AsyncTask, implement asynchronous operations in the class, provide interface feedback on the current asynchronous execution level (the ui progress can be updated through the interface), and finally feedback the execution results to the main ui thread.

1. Create a new Net.java simulation network thread, delay the program by 5 seconds

public class Net {
    public void net(){
        try {
            Thread.sleep(5000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }

    }
}

2. Create a new MyAsyncTask.java, inherit AsyncTask and realize thread asynchrony

public class MyAsyncTask extends AsyncTask {
    //A separate thread
    @Override
    protected Object doInBackground(Object[] objects) {
        //Instantiation of Net
        Net net = new Net();
        //Call net method to delay the program for 5 seconds
        net.net();
        return null;
    }
}

3. Layout two buttons in xml and execute two operations respectively -- delay program and output statement

<?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.test.androidtest.AsyncTaskActivity">

    <Button
        android:id="@+id/btnAT1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start thread"/>
    <Button
        android:id="@+id/btnAT2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Perform asynchronous operation"/>
</LinearLayout>

4. call in Activity

public class AsyncTaskActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn1,btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_task);
        btn1 = (Button)findViewById(R.id.btnAT1);
        btn2 = (Button)findViewById(R.id.btnAT2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAT1:
                //If you do not perform the AsyncTask operation, call Net.net() directly, and another thread will execute after net execution
                //Net net = new Net();
               // net.net();
                //If AsyncTask operation is performed, Net.net() is called in MyAsyncTask, and the other thread will execute directly.
                MyAsyncTask myAsyncTask = new MyAsyncTask();
                myAsyncTask.execute();
                break;
            case R.id.btnAT2:
                System.out.println("666666666666666");
        }
    }
}

Statement:
1. The knowledge comes from Netease cloud classroom - Android basic video tutorial
2. This article is only used for my own learning record. If there is any infringement, please inform me to change or delete it immediately

Topics: Android Java xml network