gpt4 book ai didi

android - AsyncTask中如何处理isCancelled

转载 作者:行者123 更新时间:2023-11-30 03:22:27 25 4
gpt4 key购买 nike

我正在尝试将这段代码添加到我的 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;
}

这是我的 doInBackGround 和我用来设置要取消的异步任务标志的代码

@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();

}

}

class LoadAllData extends AsyncTask<String, String, String> {



protected String doInBackground(String... args) {

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);

当我尝试将 isCancelled 翻译成我的异步任务时,问题就来了。我在 Downloader 下收到一个错误,上面写着“Downloader 无法解析” 我还在 publishProgress 下看到一个错误,上面写着“方法 publishProgress (String...) in the type AsyncTask is not applicable for the argument (int)”我的任务是有人可以帮助将 isCancelled 放入我的 AsyncTask 中。我还进行了研究,发现在代码中使用 isCancelled 的方法不止一种。我已经意识到处理 isCancelled 对新手来说真的很麻烦。

最佳答案

1) 要解决错误,请将 Downloader 设为该类的公共(public)静态数据变量。
2)请确认

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long>

是类声明

在您调用 cancel() 之后,isCancelled() 将返回 true,并且在您的 doInBackground 返回后执行 onCancelled 而不是 onPostExecute。该参数将在后台线程上发出中断,因此您的长时间操作将关闭。不过,我假设你在某个地方发现了它?

希望这有帮助..:)..如果它没有解决错误..请发布 logcat 详细信息

来自 SDK:

Cancelling a task

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

另外请在函数 block 的开头使用 super.onpause() 等

引用这个:- link

关于android - AsyncTask中如何处理isCancelled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18882456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com