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>
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 **
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);
AsyncTaskM<Param,Progress,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>
When an asynchronous task is executed, the task takes four steps.
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!
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.
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 ...).
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>! ..
■ Nested classes
enum |
AsiyncTask.Status |
Indicates the current status of the task td> tr> |
■ Fields public static final Executor
SERIAL_EXECUTOR |
Executor that processes tasks one by one td> tr> |
HREAD_POOL_EXECUTOR |
Executor used during parallel processing td> tr> |
■ Public constructors
AsyncTask<> |
Create new sync task td> tr> |
■ Public methods
final boolean | cancel (boolean mayInterruptIfRunning) font> Cancel task < /td> |
final AsyncTask | execute (Params ... param) font> Process task by specifying parameters td> |
static void | execute (Runnable runnable) font> execute (java.lang.Object) td> to easily process a simple Runnable object. |
final AsyncTask | executeOnExecutor (Executor font> exec, Params ... param> Process task by specifying parameters td> |
final Result | get(long timeout,TimeUnit unit) If it takes a long time to complete the process, wait and get the result td> |
final Result | get() Wait if necessary to complete the process and get the result tdd> |
final AsyncTask.Status | getStatus font> Returns the current status of the task td> |
final boolean | isCancelled() Returns true if cancellation is successful before the task completes successfully td> |
Protected methods
abstract Result | doInBackground(Params... params) A method that overrides to perform processing in a background thread td> |
void | onCancelled() Override onCancelled (java.lang.Object) font> if you can! td> |
void | onCancelled(Result result) After executing cancel (boolean) font> and doInBackground (java.lang.Object []) font> is finished Runs on UI threads td> |
void | onPostExecute(Result result) Run on UI thread after doInBackground (Params ...) font> td> |
void | onPreExecute() Run on UI thread before doInBackground (Params ...) font> td> |
void | onProgressUpdate(Progress... values) Runs on UI thread after publishProgress (Progress ...) font> is called td> |
final void | publishProgress(Progress... values) This method is called from doInBackground (Params ...) font> to update in the UI thread while processing in the background td> |
From class java.lang.Object |
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>
-@Override method (Leave the trajectory to the Android system)
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 ().
If there is any processing you want to do before executing Execute (), here!
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!
A method that is used in doInBackground () to display the result of doInBackground () on the UI thread side. Use the AsyncTask argument Result.
Please when you want to use the progress display! When you run publishProgress () on doInBackground (), the Android system runs onProgressUpdate () on the UI thread.
When you run publishProgress () on doInBackground (), the Android system runs onProgressUpdate () on the UI thread. Refer to the argument of AsyncTask for the argument
--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;
}
}
Code that changes the display of textView every 3 seconds
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);
}
}
}
MainActivity
...
new MyAsyncTask().execute("param1","param2");
MyAsyncTask
...
@Override
protected String doInBackground(String... string){
String param1 = string[0];
String param2 = string[1];
return null
}
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