Android 后退按钮只关闭进度对话框,不关闭异步任务
当用户点击后退按钮时,我正试图取消我的aynctask。我已经研究了使用哪种方法进行此操作,我发现很难找出为什么有不止一种方法。我找到了一个方法并尝试了它,但它只会取消我的ProgressDialog,我的Aynctask仍然执行。所以,有人可以帮助我解决这个问题,让我的任务以正确的方式退出Android 后退按钮只关闭进度对话框,不关闭异步任务,android,Android,当用户点击后退按钮时,我正试图取消我的aynctask。我已经研究了使用哪种方法进行此操作,我发现很难找出为什么有不止一种方法。我找到了一个方法并尝试了它,但它只会取消我的ProgressDialog,我的Aynctask仍然执行。所以,有人可以帮助我解决这个问题,让我的任务以正确的方式退出 @Override public void onBackPressed() { /** If user Pressed BackButton While Running
@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;
}
受保护的字符串doInBackground(字符串…args){
试一试{
Intent in=getIntent();
String searchTerm=in.getStringExtra(“标记搜索”);
字符串查询=URLEncoder.encode(搜索词,“utf-8”);
字符串URL=”http://example.com";
JSONPARSER jParser=新的JSONPARSER();
JSONObject json=jParser.readJSONFeed(URL);
试一试{
JSONArray questions=json.getJSONObject(“全部”).getJSONArray(“问题”);
对于(int i=0;i
我认为问题在于异步任务的doInBackground方法。使用mTask.cancel(true)取消异步任务时代码>它不会取消任务,只是设置了一个标志。如果任务已取消(标记为cancel),则需要签入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;
}
protectedlongdoinbackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
来源:我认为问题在于异步任务的doInBackground方法。使用mTask.cancel(true)取消异步任务时代码>它不会取消任务,只是设置了一个标志。如果任务已取消(标记为cancel),则需要签入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;
}
protectedlongdoinbackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
来源:您应该在进度对话框中添加一个侦听器(例如,)并取消其中的任务。您应该在进度对话框中添加一个侦听器(例如,)然后取消那里的任务。我在Downloader
下得到一个语法错误,说Downloader无法解决
我还添加了我的doInBackground
方法你看到我的评论了吗?我在Downloader
下得到一个语法错误,说Downloader无法解决
我还添加了我的doInBackground
method你看到我的评论了吗?