[JAVA] Programming from 51 years old Note AsyncTask --reference [copying sutras]

https://developer.android.com/reference/android/os/AsyncTask

It is a personal sutra-copying summary. I'm sorry if there is a clerical error.

[Multithreaded summary is here] (https://qiita.com/old_cat/items/a6d71f18a0789351c1a3)

public abstract class AsyncTask extends Object android.os.AsyncTask<Param,Progress,Result>

How is it common with post-asynchronous communication processing and generalization of AsyncTask? About

AsyncTask overview

AsyncTask handles UI threads easily and properly. Process in the background and return the result to the UI thread without using Thread or Handler.

AsyncTask is designed as a helper class that handles Thread, Handler, etc. (instantiated and used: there is no static method). It is not configured to be a popular threading framework.

AsyncTask is suitable for processing in a few seconds. If you want to thread for a long time, use APIs provided in the java.util.concurrent package such as ** Executor, ThreadPoolExecuter, FutureTask **.

Asynchronous tasks are defined by the results that are executed in the background thread and passed to the UI thread? .. That is, it is defined in the following argument types and in four steps.

-** AsyncTask arguments **

-** 4 steps **

How to use

AsyncTask is used as a subclass, overriding at least one method (** doInBackground (Param ...) ) and, in most cases, the second ( onPostExecute (Result)). **).

Below is an example of subclassing

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Once generated, task execution is really easy!

 new DownloadFilesTask().execute(url1, url2, url3);

AsyncTask argument

AsyncTaskM<Param,Progress,Result>

  1. ** Param **
    Parameter type to pass to task at runtime
  2. ** Progress **
    Type of progress unit to display during background processing
  3. ** Result **
    Type of background processing result

You don't have to use all types, you can do it with void. Like this

 private class MyTask extends AsyncTask<Void,Void,Void>

4 steps

When an asynchronous task is executed, the task takes four steps.

  1. ** onPreExecute () **
    A preparatory method to call on the UI thread before the task is processed. For example, when you want to display the progress bar when executing Execute (), it feels like preparing to display the progress bar with onPreExecute ().
  2. doInBackground (Params ...) </ font> </ strong>: Override required
    onPreExecute () Called in the background thread immediately after processing .. It is a step used for processing that it is OK even if background processing takes time. In this step, the parameters of the asynchronous task are passed. The processing result is always returned in this step, and the result is passed to the last step. This step can also be used for publishProgress (Progress), which displays progress units, in which case the onProgressUpdate (Progress ...) step will show it in the UI thread.
  3. ** onProgressUpdate (Progress ...) **
    Called by the UI thread after executing publishProgress (Progress ...). The timing of execution is undefined. This method is used to display various progress forms. The progress form is displayed in the UI during background processing. For example, an animation progress bar or a log display in a text field.
  4. ** onPostExecute (Result) **
    Called by the UI thread after the background processing is completed. The result of background processing is returned as a parameter to this step.

Task cancellation

Use cancel (boolea) to cancel the task. If you execute cancel () (if it succeeds), isCancelled () will be called later and return true. Normally, when you execute doInBackground (java.lang.Object []), onPostExecute (java.lang.Object) is executed, but when you execute cannel (), onCannelled (java.lang.Object) instead of onPostExecute () The flow of feeling that is executed. DoInBackground (java.lang.Object []) will issue a return value of isCancelled () (true if canceled!) To check if the task has been canceled properly, so check it as quickly as possible!


Thread rules

There are some rules for proper use of threads.

--AsyncTask is called on the UI thread side. This will happen automatically after the Android version is JELLY_BEAN. -** Task instance is created on the UI thread side ** -** Execute execute (Params ...) on the UI side ** --Don't call onPreExesute (), onPostExecute (Result), doInBackground (Prams ...), onProgressUpdate (Progress ...) directly. Leave it to your Android system! --The task can only be executed once. If it is executed a second time, an exception will be thrown.


Check memory

AsyncTask ensures that all callback function calls are synchronized, except in the case of explicit synchronization, to check the following:

--The memory impact of onPreExecute (), the memory impact of methods executed before calling execute (Params ...), etc. (construction of AsyncTask object) are displayed in doInBackground (Params ...).

  • doInBackground(Params...) The impact on memory is shown in onPostExecute (Result) --The memory impact of doInBackgrund (Params ...) before calling publishProgress (Progres ...) is shown in the corresponding onProgressUpdate (Progress ..). However, doInBackground (Params ...) will continue to run, so the doInBackground (Params ...) update is in progress onProgressUpdata (Progress ...) Be careful not to interfere with the call. --The memory impact of cancel (boolean) is visible after a call to isCancelled () (returns true in the execution result) that is called prior to calquek (boolean), or during or after calling onCancelled (). I will.

Background of AsyncTask

Initially, AsyncTasks was processed sequentially in a single thread in the background. From Build.VERSION_CODES.DONUT, the Android version has changed to a thread pool to handle multitasking that processes in parallel. Starting with the Android version Build.VERSION_CODES.HONEYCOMB, tasks are run in a single thread to avoid application errors caused by concurrency.

If you want to do concurrency </ font>, ** THREAD_POOL_EXECUTOR ** and ** executeOnExecutor ** (java.util.concurrent.Executor) Execute, java.lang.Object []) </ font>! ..

Overview

■ Nested classes

enum
AsiyncTask.Status
Indicates the current status of the task

■ Fields public static final Executor

SERIAL_EXECUTOR
Executor that processes tasks one by one
HREAD_POOL_EXECUTOR
Executor used during parallel processing

■ Public constructors

AsyncTask<>
Create new sync task

■ Public methods

final boolean cancel (boolean mayInterruptIfRunning)
Cancel task < /td>
final AsyncTask execute (Params ... param)
Process task by specifying parameters
static void execute (Runnable runnable)
execute (java.lang.Object) to easily process a simple Runnable object.
final AsyncTask executeOnExecutor (Executor exec, Params ... param>
Process task by specifying parameters
final Result get(long timeout,TimeUnit unit)
If it takes a long time to complete the process, wait and get the result
final Result get()
Wait if necessary to complete the process and get the result
final AsyncTask.Status getStatus
Returns the current status of the task
final boolean isCancelled()
Returns true if cancellation is successful before the task completes successfully

Protected methods

abstract Result doInBackground(Params... params)
A method that overrides to perform processing in a background thread
void onCancelled()
Override onCancelled (java.lang.Object) if you can!
void onCancelled(Result result)
After executing cancel (boolean) and doInBackground (java.lang.Object []) is finished Runs on UI threads
void onPostExecute(Result result)
Run on UI thread after doInBackground (Params ...)
void onPreExecute()
Run on UI thread before doInBackground (Params ...)
void onProgressUpdate(Progress... values)
Runs on UI thread after publishProgress (Progress ...) is called
final void publishProgress(Progress... values)
This method is called from doInBackground (Params ...) to update in the UI thread while processing in the background
Inherited method
From class java.lang.Object

AsyncTask Summary

Use AsyncTask as a subclass AsyncTask<Params,Progress,Result>

--Params
Specify the argument type to be used in doInBackground () --Progress
Method type to display running information to user
Specify argument type to be used in publishProgress (), onProgressUpdate () --Result
Result display method
Specify the argument type to be used in onPostExecute ()

If AsyncTask requires no arguments, AsyncTask \ <void, void, void>

Main methods

  • execute()
  • publishProgress()

-@Override method (Leave the trajectory to the Android system)

  • onPreExecute()
  • doInBackground()
  • onProgressUpdate()
  • onPostExecute()
・ Execute (Params ... params)

Method to be executed on the UI thread side. Execute doInBackground by passing an argument to the override method doInBackground ()! ..

For the argument type, refer to the argument type of AsyncTask. The argument is used to execute doInBackground ().

・ OnPreExecute

If there is any processing you want to do before executing Execute (), here!

・ DoInBackground ()

Override required method! </ font> Receives arguments from execute () and processes tasks in the background. Call onPostExecute () after doInBackground () processing and pass the result of doInBackground () processing to onPostExecute (). If you use cancel (), onCannelled () will be called instead of onPostExecute (). doInBackground () is for task processing only. Update the UI thread with other methods!

・ OnPostExecute ()

A method that is used in doInBackground () to display the result of doInBackground () on the UI thread side. Use the AsyncTask argument Result.

・ PublishedProgress ()

Please when you want to use the progress display! When you run publishProgress () on doInBackground (), the Android system runs onProgressUpdate () on the UI thread.

・ OnProgressUpdate ()

When you run publishProgress () on doInBackground (), the Android system runs onProgressUpdate () on the UI thread. Refer to the argument of AsyncTask for the argument


Sample code: execute () and doInBackground ()

--Subclass MainActivity.java
AsyncTask <> and execute execute () --Extend AsyncTask <> class to generate MyAsyncTask class
doInBackground () executes the process by execute () of MainActivity.java (to do). doInBackground () receives an argument from execute () and uses it for processing.

public class MainActivity extends AppCompatActivity {

    public static TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Create a task instance. execute execute()Argument onInBackground()Pass to
        // new MyAsyncTask().execute();But OK
        MyAsyncTask myAsyncTask = new MyAsyncTask();
        myAsyncTask.execute();

    }
}

class MyAsyncTask extends AsyncTask<Void,Void,Void> {
    @Override
    protected Void doInBackground(Void... aVoid) {
        // todo
        Log.d("msg","In MyAsyncTask");
        return null;
    }
}

Sample code: Try putting in some methods

Code that changes the display of textView every 3 seconds

  • MyActivity.onCreate()
    myAsncTask.excute("In doInBackground")
  • class MyAsyncTask
    • onPreExecute()
      textView.setText("onPreExecute")
    • doInBackground()
      sleep(10003) -> publishProgress(string[0]) -> sleep(10003) -> return "End doInBackground"
    • onProgressUpdate()
      textView.setText(string[0])
    • onPostExecute()
      textView.setText(s)
public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);

        //Create a task instance. execute execute()Argument onInBackground()Pass to
        // new MyAsyncTask<>.execute();But OK
        MyAsyncTask myAsyncTask = new MyAsyncTask();
        // doInBackground()String to("In doInBackground")Pass
        // onPreExecute()After processing doInBackground()Run
        myAsyncTask.execute("In doInBackground");

    }

    class MyAsyncTask extends AsyncTask<String,String, String> {

        // execute("In doInBackground")Execute before processing. Displayed in textView of UI thread
        @Override
        protected void onPreExecute(){
            textView.setText("onPreExecute");
        }

        // execute()String from"In doInBackground"To receive
        @Override
        protected String doInBackground(String... string) {

            //Wait 3 seconds
            try {
                sleep(1000*3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //Display textView during doInBackground processing onPublishUpdate()Call
            publishProgress(string[0]);

            //Wait 3 seconds
            try {
                sleep(1000*3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // onPostExecute()String to"End doInBackground"give
            return "End doInBackground";
        }

        //publishProgress in doInBackground()Receives a string from and displays it in the textView of the UI thread
        protected void onProgressUpdate(String... string){
            textView.setText(string[0]);
        }

        // doInBackground()Return value from("End doInBackground")And display it in the textView of the UI thread
        @Override
        protected void onPostExecute(String s){
            textView.setText(s);
        }
    }
}

Pass parameters from Activity to AsyncTask

Pass multiple parameters one by one

MainActivity


...
new MyAsyncTask().execute("param1","param2");

MyAsyncTask


...
@Override
protected String doInBackground(String... string){
   String param1 = string[0];
   String param2 = string[1];
return null
}

Pass multiple parameters as an array

MainActivity


Map<String,String> map = new HashMap<String,String>();
   map.put("param1","data1");
   map.put("param2","data2");
   map.put("param3","data3");

new MyAsyncTask().execute(map);

MyAsyncTask


public class MyAsyncTask extends AsyncTask<Map<String,String>,Void,Map<String,String>>{

   @Override
   protected Map<String,String> doInBackground(Map<String,String>... map){
      Map<String,String> mp = map[0];
      //If mp is Json
      // JSONObject jsonMap = new JSONObject(mp);
      return mp;
   }

   @Override
   protected void onPostExecute(Map<String,String> mp){
      super.onPostExecute(mp);
      String val1 = mp.get("param1"); //data1 was stored in val1
      String val2 = mp.get("param1"); //data2 was stored in val2
      String val3 = mp.get("param3"); //data3 was stored in val3
   }
}

It took a while. The turtle's walk as usual.

Recommended Posts