Android onBackPressed only dismissing ProgressDialog
我意识到我的任务有点问题。我意识到,当我按下android设备上的“上一步”按钮关闭我的进度对话框和异步任务时,只有我的进度对话框被关闭,异步任务仍在执行。我真的不知道为什么会这样,所以我只是希望有人能让我回到正确的轨道上,帮助我解决这个问题Android onBackPressed only dismissing ProgressDialog,android,Android,我意识到我的任务有点问题。我意识到,当我按下android设备上的“上一步”按钮关闭我的进度对话框和异步任务时,只有我的进度对话框被关闭,异步任务仍在执行。我真的不知道为什么会这样,所以我只是希望有人能让我回到正确的轨道上,帮助我解决这个问题 @Override public void onBackPressed() { /** If user Pressed BackButton While Running Asynctas
@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();
}
}
protected String doInBackground(String... args) {
if (isCancelled()){break;}
try {
Intent in = getIntent();
String searchTerm = in.getStringExtra("TAG_SEARCH");
String query = URLEncoder.encode(searchTerm, "utf-8");
String URL = "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);
//JSONArray Answers = question.getJSONObject(TAG_ANSWERS).getJSONArray(TAG_ANSWER);
//JSONObject Answer = Answers.getJSONObject(0);
//String Content = Answer.getString(TAG_ANSWERS_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 TAG_QUESTION ;
}
如果要取消其执行,必须在
AsyncTask
sdoInBackground
方法中实现取消功能
调用cancel()
后将返回true
,并且在执行doInBackground
后返回的onCancelled
而不是onPostExecute
。该参数将在后台线程上发出中断,因此您的长时间操作将关闭。不过,我想你是在什么地方发现的
从:
为了确保任务尽快取消,您应该
始终定期从中检查isCancelled()
的返回值
doInBackground(Object[])
,如果可能的话(例如在循环中)
那么,您想要什么,您想要完全取消异步任务吗?或者您想要使progressDialog在按下时不可取消?@shre202是的,我想要异步任务完全取消,并在用户单击“后退”按钮时将用户返回主活动。请注意格式,这也有助于防止错误。不要使用标签!谢谢你的回答。。我一直在环顾四周,想知道如何编写这个
isCancelled()
是我的doInBackGround
方法,但我就是无法用手去理解它。因此,如果没有教程,您认为您可以帮助编写此代码吗?实际实现有多种模式,它们取决于您的AsyncTask
。如果经常执行循环,请添加类似于If(isCancelled()){break;}
的内容。如果您执行了可中断的操作,请处理。我已将我的doInBackGround
方法添加到问题中,并已添加If((isCancelled())(break;}
在该代码中,但我想看看我是否将其放置在正确的位置,或者是否还有其他代码可以放置在那里,因为我将我的完整方法发布到了非指定位置。哪些部分需要长时间执行?另外,当我使用HTTPGet
方法连接到web服务时,JSONPasser
是一个类我的doInBackGround
方法只是解析数据…你想让我发布jsonparser
类吗?
public class JSONParsser {
InputStream is = null;
JSONObject jObj = null;
String json = "";
public EditText et;
public JSONParsser () {
}
public JSONObject readJSONFeed(String URL) {
try{
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(URL);
//request.setURI(website);
try {
HttpResponse response = client.execute(request);
HttpEntity httpEntity = response.getEntity();
is = httpEntity.getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
Log.d("JSON String",json);
return jObj;
}finally{}
}{
}
}