2013-09-11 3 views
0

사용자가 뒤로 버튼을 눌렀을 때 내 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; 

} 
+0

당신은 (예를 들어 진행 대화 상자에 리스너를 추가해야 [한 OnDismissListener] (http://developer.android.com/reference/android/ content/DialogInterface.OnDismissListener.html)) 작업을 취소하십시오. – ozbek

답변

3

doInBackGround 방법을 게시해야하는 경우 알려 주시기 바랍니다. mTask.cancel(true);으로 AsyncTask를 취소하면 작업을 취소하지 않고 단지 플래그를 설정합니다. 작업이 취소 된 경우 (취소로 표시된 경우) DoInBackground 메서드를 체크 인하 고 있으면이를 반환해야합니다.

예 :

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; 
    } 

출처 : http://developer.android.com/reference/android/os/AsyncTask.html

+0

'Downloader'에서 'Downloader can not be resolved'라는 문법 오류가 발생했습니다. –

+0

내 doLowground 메서드를 추가했습니다. –

+0

내 의견이 보입니까? –

관련 문제