后退按钮仅解除进度对话框而不是AsyncTask

时间:2013-09-11 00:47:48

标签: android

当用户点击后退按钮时,我试图取消我的aynctask。我已经研究了用于此操作的方法,我发现很难找出为什么有多种方法。我找到了一个方法并尝试了它,但它只解除了我的ProgressDialog并且我的aynctassk仍在执行。因此,有人可以帮助我解决这个问题,让我的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();

  }  

}

请告诉我如果我需要将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;

}

1 个答案:

答案 0 :(得分:3)

我认为问题出在你的AsyncTask的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