테스트 할 수 있습니다. 그리고 그렇습니다. execute가 호출되면 forynctask가 forground 또는 UI와 관련된 작업을 수행하지 않는 한 Asynctask가 여전히 실행되는 것을 볼 수 있습니다. (런처가 충돌 할 수 있습니다).
그러나 시스템에 가까운 경우. 메소드 실행을 계속할 수도,하지 않을 수도 있습니다. 나는 이미 시험하고 대답했다 here.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Worker().execute();
}
private class Worker extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... arg0) {
Log.i("SomeTag",
"start do in background at " + System.currentTimeMillis());
String data = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"https://stackoverflow.com/questions/tagged/android");
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
data = EntityUtils.toString(httpEntity);
Log.i("SomeTag",
"doInBackGround done at " + System.currentTimeMillis());
} catch (Exception e) {
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i("SomeTag", System.currentTimeMillis()/1000L
+ " post execute \n" + result);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("SomeTag", System.currentTimeMillis()/1000L + " onDestory()");
}
04-24 21:42:57.981: I/SomeTag(5961): start do in background at 1366854177994
04-24 21:43:00.974: I/SomeTag(5961): 1366854180 onDestory()
04-24 21:43:02.946: I/SomeTag(5961): doInBackGround done at 1366854182946
04-24 21:43:02.946: I/SomeTag(5961): 1366854182 post execute
04-24 21:43:02.946: I/SomeTag(5961): <!DOCTYPE html>
04-24 21:43:02.946: I/SomeTag(5961): <html>
04-24 21:43:02.946: I/SomeTag(5961): <head>
04-24 21:43:02.946: I/SomeTag(5961):
04-24 21:43:02.946: I/SomeTag(5961): <title>Newest 'android' Questions - Stack Overflow</title>
04-24 21:43:02.946: I/SomeTag(5961): <link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico">
//....
당신은'AsyncTask를의 cancel' 방법을 사용하여 작업을 실행 취소 활동의 라이프 사이클을 사용해야합니다. UI가 파괴 되더라도 계속 실행될 가능성이있는 네트워크 작업을 계속하는 것은 쓸모가 없습니다. 그러나 네트워크 운영이 완료되면 UI 앱을 업데이트하려고 할 때 문제가 발생합니다. – minhaz