取消的AsyncTask本身内 [英] Cancel an AsyncTask inside itself

查看:138
本文介绍了取消的AsyncTask本身内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题如下,我有一个的AsyncTask类,从preference文件即调用。这样做的目的是从一个XML文件中调用。这AsyncTask的类是连续运行的,因为我做的一个房间内的声音的实时FFT,只会被取消,如果我打的返回按钮。我不能检查键在pressed因为此类扩展AsyncTask的,所以我不能正确地结束线程,以便达到其onCancelled()的状态。我能想到的结束这场唯一的另一种方法是使这是从XML文件又调用这个类并调用类对象的.cancel(真)称为备用类..解决这个任何其他的想法?

So my problem is as follows, I have an AsyncTask Class that is called from a preference file ie. The intent is called from within an xml file. This AsyncTask Class is continuously running as I am doing a real time FFT of the sound within a room and will only be cancelled if I hit the return button. I cant check for keys being pressed as this Class extends AsyncTask and therefore I cant end the thread properly so that it reaches its onCancelled() state. The only other way I can think about ending this is to make a standby class which is called from the xml file which in turn calls this class and calls the .cancel(true) on the class object.. Any other ideas around this?

推荐答案

可以张贴一些code?

Can you post some code?

在您的调用方法(其中你也许可以检查键),就可以调用 myASyncTask.cancel(),然后在您的AsyncTask的doInBackground()方法,你可以检查 isCancelled()再破你的循环,并返回到您的重写 onCancelled()您的AsyncTask的方法。

In your calling method (in which you might be able to check for keys), you can call myASyncTask.cancel() and then in the doInBackground() method of your AsyncTask, you can check for isCancelled() and then break your loop and return to your overridden onCancelled() method of your AsyncTask.

http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)

帮助吗?

这篇关于取消的AsyncTask本身内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆