AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
AsyncTask(비동기 작업)는 UI 스레드를 쉽고 적절하게 사용할 있도록 해준다. 해당 클래스는 스레드나 Handler(핸들러)에 대한 별도의 조정없이 백그라운드 작업과 UI 스레드에 결과를 나타내는 것이 가능하다.
AsyncTask(비동기 작업)는 스레드와 Handler(핸들러)를 옆에서 도와주는 클래스로서 만들어졌고, 포괄적인 스레딩 체계를 이루지는 않는다. AsyncTasks(비동기 작업들)은 이상적으로 (기껏해서 몇 초의) 짧은 동작에만 사용해야 한다. 긴 시간동안 쓰레드를 계속 구동시켜야 한다면, java.util.concurrent에서 제공하는 Executor, ThreadPoolExecutor 그리고 FutureTask와 같은 다양한 API들을 사용할 것을 강력히 추천한다. 비동기 작업은 백그라운드 스레드에서 실행되는 처리 과정과 UI 스레드에 나타나는 그 결과로 정의된다.
비동기 작업은 매개변수, 진행과정 그리고 결과라는 3가지의 일반 유형과 onPreExecute, doInBackground, onProgressUpdate, onPostExecute의 4가지 단계로 정의된다.
The basic methods used in an android AsyncTask class are defined below :
-
doInBackground() : This method contains the code which needs to be executed in background. In this method we can send results multiple times to the UI thread by publishProgress() method. To notify that the background processing has been completed we just need to use the return statements
-
onPreExecute() : This method contains the code which is executed before the background processing starts
-
onPostExecute() : This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method
-
onProgressUpdate() : This method receives progress updates from doInBackground method, which is published via publishProgress method, and this method can use this progress update to update the UI thread
The three generic types used in an android AsyncTask class are given below :
-
Params : The type of the parameters sent to the task upon execution
-
Progress : The type of the progress units published during the background computation
-
Result : The type of the result of the background computation
📄 AsyncTask Basic Source Code
/* @Parameter ① doInBackground()에게 넘겨줄 변수 Type ② onProgressUpdate()에서 사용할 변수 Type ③ onPostExecute()에서 사용할 변수 Type */
private class AndoidAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... urls) {
return;
}
@Override
protected void onProgressUpdate(Void... progress) {
}
@Override
protected void onPostExecute(Void result) {
}
}
🚀 REFERENCE
'#모바일 [Mobile] > Android' 카테고리의 다른 글
[Android] Custom Dialog 띄우기 (How to create a Custom Dialog in android?) (0) | 2019.08.07 |
---|---|
[Android] ButterKnife Proguard 설정하기 (How to configure Proguard settings for ButterKnife?) (0) | 2019.08.01 |
[Android - OpenSource] Butter Knife (0) | 2019.07.25 |
[Android] Ripple Drawable (버튼 눌림 효과, 물결 효과) (0) | 2019.07.24 |
[Android] Date Picker Dialog (0) | 2019.07.22 |
댓글