0

当用户点击后退按钮时,我试图取消我的 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;

}
4

1 回答 1

3

我认为问题出在 AsyncTask 的 doInBackground 方法中。当您取消 AsyncTask 时,mTask.cancel(true);它不会取消任务,它只是设置一个标志。如果任务已被取消(标记为取消),则需要检查 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

于 2013-09-11T01:20:49.847 回答