簡體   English   中英

“后退”按鈕僅關閉“進度對話框”,而不關閉AsyncTask

[英]Back button only dismissing Progress Dialog and not the AsyncTask

當用戶單擊“后退”按鈕時,我試圖取消我的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;

}

我認為問題出在AsyncTask的doInBackground方法中。 當您使用mTask.cancel(true);取消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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM