AsyncTask에 전달되는 인수?
나는 여기에 무엇을 넣어야 하고이 논쟁이 끝나는 곳을 이해하지 못합니까? 정확히 무엇을 넣어야하며 어디로 정확히 갈까요? 3을 모두 포함해야합니까, 아니면 1,2,20을 포함 할 수 있습니까?
구글의 안드로이드 문서는 말한다 :
비동기 작업은 Params, Progress and Result라는 3 가지 일반 유형과 onPreExecute, doInBackground, onProgressUpdate 및 onPostExecute라는 4 단계로 정의됩니다.
AsyncTask의 일반 유형 :
비동기 작업에 사용되는 세 가지 유형은 다음과 같습니다.
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.
모든 유형이 항상 비동기 작업에서 사용되는 것은 아닙니다. 유형을 사용하지 않는 것으로 표시하려면 Void 유형을 사용하십시오.
private class MyTask extends AsyncTask<Void, Void, Void> { ... }
추가 참조 : http://developer.android.com/reference/android/os/AsyncTask.html
또는 Sankar-Ganesh의 블로그를 참조하여 AsyncTask의 역할을 지울 수 있습니다.
일반적인 AsyncTask 클래스의 구조는 다음과 같습니다.
private class MyTask extends AsyncTask<X, Y, Z>
protected void onPreExecute(){
}
이 메소드는 새 스레드를 시작하기 전에 실행됩니다. 입력 / 출력 값이 없으므로 변수 또는 필요한 것을 초기화하십시오.
protected Z doInBackground(X...x){
}
AsyncTask 클래스에서 가장 중요한 방법입니다. 여기에서 원하는 모든 것을 백그라운드에서 기본 스레드와 다른 스레드에 배치해야합니다. 여기에 우리는 입력 값으로“X”유형의 객체 배열을 가지고 있습니다 (헤더에 표시됩니까?“... extends AsyncTask”입력 매개 변수의 유형입니다). 유형에서 객체를 반환합니다. "지".
protected void onProgressUpdate(Y y){
}
이 메소드는 publishProgress (y) 메소드를 사용하여 호출되며 일반적으로 백그라운드에서 수행중인 조작의 진행률을 표시하는 진행률 표시 줄과 같이 진행률 또는 정보를 기본 화면에 표시하려는 경우에 사용됩니다.
protected void onPostExecute(Z z){
}
이 메소드는 백그라운드에서 조작이 완료된 후에 호출됩니다. 입력 매개 변수로 doInBackground 메소드의 출력 매개 변수를받습니다.
X, Y 및 Z 유형은 어떻습니까?
위의 구조에서 추론 할 수 있듯이 :
X – The type of the input variables value you want to set to the background process. This can be an array of objects.
Y – The type of the objects you are going to enter in the onProgressUpdate method.
Z – The type of the result from the operations you have done in the background process.
우리는이 과제를 외부 수업에서 어떻게 부릅니까? 다음 두 줄만 있으면됩니다.
MyTask myTask = new MyTask();
myTask.execute(x);
여기서 x는 X 유형의 입력 매개 변수입니다.
작업이 실행되면 "외부"에서 상태를 확인할 수 있습니다. "getStatus ()"메소드 사용
myTask.getStatus();
다음 상태를 수신 할 수 있습니다.
RUNNING- 작업이 실행 중임을 나타냅니다.
PENDING- 작업이 아직 실행되지 않았 음을 나타냅니다.
FINISHED -onPostExecute (Z)가 완료되었음을 나타냅니다.
AsyncTask 사용에 대한 힌트
onPreExecute, doInBackground 및 onPostExecute 메소드를 수동으로 호출하지 마십시오. 이것은 시스템에 의해 자동으로 수행됩니다.
다른 AsyncTask 또는 스레드 내에서 AsyncTask를 호출 할 수 없습니다. 메소드 실행의 호출은 UI 스레드에서 수행해야합니다.
onPostExecute 메소드는 UI 스레드에서 실행됩니다 (여기서는 다른 AsyncTask를 호출 할 수 있습니다!).
작업의 입력 매개 변수는 Object 배열이 될 수 있으므로 원하는 개체와 유형을 모두 넣을 수 있습니다.
나는 파티에 너무 늦었지만 이것이 누군가를 도울 것이라고 생각했다.
간단하게 유지하십시오!
An AsyncTask
is background task which runs in the background thread. It takes an Input, performs Progress and gives Output.
ie
AsyncTask<Input,Progress,Output>
.
In my opinion the main source of confusion comes when we try to memorize the parameters in the AsyncTask
.
The key is Don't memorize.
If you can visualize what your task really needs to do then writing the AsyncTask
with the correct signature would be a piece of cake.
Just figure out what your Input, Progress and Output are and you will be good to go.
Heart of the AsyncTask!
doInBackgound()
method is the most important method in an AsyncTask
because
- Only this method runs in the background thread and publish data to UI thread.
- Its signature changes with the
AsyncTask
parameters.
So lets see the relationship
doInBackground()
andonPostExecute()
,onProgressUpdate()
are also related
Show me the code
So how will I write the code for DownloadTask?
DownloadTask extends AsyncTask<String,Integer,String>{
@Override
public void onPreExecute()
{}
@Override
public String doInbackGround(String... params)
{
// Download code
int downloadPerc = // calculate that
publish(downloadPerc);
return "Download Success";
}
@Override
public void onPostExecute(String result)
{
super.onPostExecute(result);
}
@Override
public void onProgressUpdate(Integer... params)
{
// show in spinner, access UI elements
}
}
How will you run this Task
new DownLoadTask().execute("Paradise.mp3");
Refer to following links:
- http://developer.android.com/reference/android/os/AsyncTask.html
- http://labs.makemachine.net/2010/05/android-asynctask-example/
You cannot pass more than three arguments, if you want to pass only 1 argument then use void for the other two arguments.
1. private class DownloadFilesTask extends AsyncTask<URL, Integer, Long>
2. protected class InitTask extends AsyncTask<Context, Integer, Integer>
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.
KPBird
in Short, There are 3 parameters in AsyncTask
parameters for Input use in DoInBackground(String... params)
parameters for show status of progress use in OnProgressUpdate(String... status)
parameters for result use in OnPostExcute(String... result)
Note : - [Type of parameters can vary depending on your requirement]
참고URL : https://stackoverflow.com/questions/6053602/what-arguments-are-passed-into-asynctaskarg1-arg2-arg3
'development' 카테고리의 다른 글
ASP.NET에서 페이지를 어떻게 새로 고치나요? (0) | 2020.06.08 |
---|---|
Android 앱에서 Facebook 페이지를여시겠습니까? (0) | 2020.06.08 |
모든 사용자 테이블을 삭제하는 방법? (0) | 2020.06.08 |
열에서 여러 데이터 프레임을 결합하는 팬더 3 방향 (0) | 2020.06.08 |
AngularJS의 범위에서 항목을 제거하는 방법은 무엇입니까? (0) | 2020.06.08 |