사용자가 뒤로 버튼을 눌렀을 때 내 aynctask를 취소하려고합니다. 나는이 작업을 위해 어떤 방법을 사용할 지에 대한 연구를 해왔고 왜 하나 이상의 방법이 존재하는지 파악하기가 어려웠습니다. 한 가지 방법을 찾아서 시도했지만 ProgressDialog 만 해고하고 내 aynctassk는 여전히 실행됩니다. 그래서 누군가가 적절한 방법을 기각하기 위해 내 asynctask를 얻는이 문제에 대해 저를 도울 수 있습니다.뒤로 버튼은 AsyncTask가 아니라 진행 대화 상자를 닫습니다.
@Override
public void onBackPressed()
{
/** If user Pressed BackButton While Running Asynctask
this will close the ASynctask.
*/
if (mTask != null && mTask.getStatus() != AsyncTask.Status.FINISHED)
{
mTask.cancel(true);
}
super.onBackPressed();
finish();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
/** If Activity is Destroyed While Running Asynctask
this will close the ASynctask. */
if (mTask != null && mTask.getStatus() != AsyncTask.Status.FINISHED)
{
mTask.cancel(true);
}
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
if (pDialog != null)
{
if(pDialog.isShowing())
{
pDialog.dismiss();
}
super.onPause();
}
}
그리고 내가 문제가 AsyncTask를의 doInBackground 방법에 생각하는 질문
protected String doInBackground(String... args) {
try {
Intent in = getIntent();
String searchTerm = in.getStringExtra("TAG_SEARCH");
String query = URLEncoder.encode(searchTerm, "utf-8");
String URL = "http://example.com";
JSONParsser jParser = new JSONParsser();
JSONObject json = jParser.readJSONFeed(URL);
try {
JSONArray questions = json.getJSONObject("all").getJSONArray("questions");
for(int i = 0; i < questions.length(); i++) {
JSONObject question = questions.getJSONObject(i);
String Subject = question.getString(TAG_QUESTION_SUBJECT);
String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER);
String Content = question.getString(TAG_QUESTION_CONTENT);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_QUESTION_SUBJECT, Subject);
map.put(TAG_QUESTION_CONTENT, Content);
map.put(TAG_QUESTION_CHOSENANSWER, ChosenAnswer);
questionList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
당신은 (예를 들어 진행 대화 상자에 리스너를 추가해야 [한 OnDismissListener] (http://developer.android.com/reference/android/ content/DialogInterface.OnDismissListener.html)) 작업을 취소하십시오. – ozbek