如何在android中取消Asysnctask
我正在应用程序的UI部分运行一些任务 在执行这项冗长的任务时,我需要显示“进度”对话框,因此我使用AsysnTask来执行此任务 当用户在进度条上按下后退按钮时,我需要取消进度条,并暂停正在进行的后台工作 我在“进度”对话框中调用了“disclose()”,因此它被取消 我在我的AsysnTask对象上调用“cancel(true)”,我认为我的后台操作已经停止,但它仍然在执行,尽管我调用了“cancel(true)” 然后我对这个问题进行了一些搜索,发现“cancel(true)”只调用了AsyncTask的“onCancelled()”方法 我不知道我必须在onCancelled()方法中编写什么,以便立即停止后台操作 我的doInBackground()方法如下所示:如何在android中取消Asysnctask,android,Android,我正在应用程序的UI部分运行一些任务 在执行这项冗长的任务时,我需要显示“进度”对话框,因此我使用AsysnTask来执行此任务 当用户在进度条上按下后退按钮时,我需要取消进度条,并暂停正在进行的后台工作 我在“进度”对话框中调用了“disclose()”,因此它被取消 我在我的AsysnTask对象上调用“cancel(true)”,我认为我的后台操作已经停止,但它仍然在执行,尽管我调用了“cancel(true)” 然后我对这个问题进行了一些搜索,发现“cancel(true)”只调用了As
protected void doInBackground()
{
Statement 1;
Statement 2;
Statement 3;
Statement 4;
Statement 5;
Statement 6;
Statement 6;
Statement 7;
Statement 8;
Statement 9;
}
假设在上面的代码中,如果在“语句4”之后得到取消请求,那么我不想执行铰语句
上面的“doInBackground()”只是一个可以理解的例子,实际上我的“doInBackground()”方法将有100条语句,我无法预测何时可以得到取消操作的请求
请帮助我如何做。异步任务有两种重要的方法用于此目的:
cancel()
和isCancelled()
在doInBackground()
中的某个地方,应该检查isCancelled()
的值,如果返回true,则完成执行。与正常执行的一个重要区别是,一旦doInBackground()
返回,将在UI线程上调用onCancelled()
,而不是onPostExecute()
例如,文档中简单任务的修改版本:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count && !isCancelled(); i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
protected void onCancelled(){
showDialog("Operation was cancelled");
}
}
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
对于(int i=0;i
看看官方文档中的“取消任务”:AsyncTasks有两种重要的方法用于此目的:
cancel()
和isCancelled()
在doInBackground()
中的某个地方,应该检查isCancelled()
的值,如果返回true,则完成执行。与正常执行的一个重要区别是,一旦doInBackground()
返回,将在UI线程上调用onCancelled()
,而不是onPostExecute()
例如,文档中简单任务的修改版本:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count && !isCancelled(); i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
protected void onCancelled(){
showDialog("Operation was cancelled");
}
}
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
对于(int i=0;i
看看官方文档中的“取消任务”:java中的线程从不过早终止。您可以使用本机API杀死线程,但Java API不再允许这样做(尽管还有一些函数公开此接口,但它们没有实现,只是抛出异常) Android暴露了同样的理念,线程不能终止。作为开发人员,您有责任以这种方式编写线程,因此当线程被取消时,它会做出正确的反应 在您的情况下,您需要实现
doInBackground()
,以便它检查线程是否已被取消。例如:
protected Void doInBackground()
{
if(isCancelled()) return null;
Statement 1;
Statement 2;
if(isCancelled()) return null;
Statement 3;
if(isCancelled()) return null;
Statement 9;
}
这只是一个例子,您可以制作一个更好的实现,在执行下一个任务之前检查线程状态
顺便说一句,它是专门为这样的用例设计的。从文件:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count && !isCancelled(); i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
protected void onCancelled(){
showDialog("Operation was cancelled");
}
}
取消公共最终布尔值()
如果此任务在正常完成之前被取消,则返回true。
如果对任务调用cancel(布尔值),则
应定期从以下位置检查此方法:
doInBackground(Object[])以尽快结束任务
在java中,线程从不过早终止。您可以使用本机API杀死线程,但Java API不再允许这样做(尽管还有一些函数公开此接口,但它们没有实现,只是抛出异常) Android暴露了同样的理念,线程不能终止。作为开发人员,您有责任以这种方式编写线程,因此当线程被取消时,它会做出正确的反应 在您的情况下,您需要实现
doInBackground()
,以便它检查线程是否已被取消。例如:
protected Void doInBackground()
{
if(isCancelled()) return null;
Statement 1;
Statement 2;
if(isCancelled()) return null;
Statement 3;
if(isCancelled()) return null;
Statement 9;
}
这只是一个例子,您可以制作一个更好的实现,在执行下一个任务之前检查线程状态
顺便说一句,它是专门为这样的用例设计的。从文件:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count && !isCancelled(); i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
protected void onCancelled(){
showDialog("Operation was cancelled");
}
}
pub