用BetterAsyncTask(Droid-Fu)弥补AsyncTask的缺点
在 google 上进行了长时间且不成功的搜索后,结果发现几乎没有任何关于名为 Droid-Fu 的便捷库的信息 https://github.com/kaeppler/droid-fu
阅读创建者的介绍后(http://brainflush.wordpress.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/)或 API (< a href="http://kaeppler.github.com/droid-fu" rel="nofollow">http://kaeppler.github.com/droid-fu),我可以不知道如何定义一个新的更好的异步任务(什么方法保存什么信息等)。
因此,如果有人可以为我(显然还有其他人)提供一些有用的源代码或教程,我将不胜感激!
(如果您需要项目的 jar 文件,请告诉我,我可以给您发送一份)
编辑:
好的,这是我在源代码中找到的一些附加信息:
- 自动显示进度对话框。请参阅 useCustomDialog()、disableDialog()
- 如果从 doInBackground 内部抛出异常,则现在由 handleError 方法处理。
- 现在您应该不再重写 onPreExecute()、doInBackground() 和 onPostExecute(),而是应该分别使用 before()、doCheckedInBackground() 和 after()。
让我们看看从这里开始我能实现什么......不过仍在寻找一个可行的示例!
编辑2:
可以找到几个示例此处 和 这里。我坚持这样做,但出现错误。唯一的区别是我的 AsyncTask 不是在活动中定义的,而是它自己的一个类。单步执行代码显示错误发生在创建(AsyncTask 内置)对话框时。
这是我的堆栈跟踪:
一分钟后就会出现
After a lengthy and unsuccessful search on google, it turns out that there is hardly any information about a handy library called Droid-Fu https://github.com/kaeppler/droid-fu
After reading the introduction by the creator (http://brainflush.wordpress.com/2009/11/16/introducing-droid-fu-for-android-betteractivity-betterservice-and-betterasynctask/) or the API (http://kaeppler.github.com/droid-fu), I could not figure out how to define a new betterasynctask (what methods hold what information etc).
So if there is anyone out there that could provide me (and apperently others as well) with some useful source code or tutorials, I would greatly appreciate it!
(If you need the jar file of the project, let me know, I can send you a copy)
EDIT:
A good example can be found here! and here
Ok, here is some additional information I found in the source code:
- A progress dialog is automatically shown. See useCustomDialog(), disableDialog()
- If an Exception is thrown from inside doInBackground, this is now handled by the handleError method.
- You should now longer override onPreExecute(), doInBackground() and onPostExecute(), instead you should use before(), doCheckedInBackground() and after() respectively.
Let's see what I can achieve from here on then...still looking for a working example though!
EDIT 2:
A couple of examples can be found here and here. I stick to it but I get an error. Only difference is that my AsyncTask is not defined within the activity, but a class of its own. Stepping through the code reveals that the error happens upon creation of the (AsyncTask built-in) Dialog.
This is my stacktrace:
coming in a minute
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Droid-fu 现在有点过时了,主要是缺乏 Fragment 支持。但我会举一个我编写的使用它的应用程序的例子。
首先,您的活动类必须是 BetterActivity (或 BetterXXXActivity)的子类。在我的代码中,我使用了 ListActivity,因此我在这里创建了 BetterListActivity 的子类。我还定义了 BetterAsyncTask 的子类,以便可以扩展一些功能。
现在,我的任务需要不带参数,使用不确定的对话框,因此不会发布进度,并且需要返回一个整数。您的需求可能有所不同,这会影响类定义中使用的类型。
下一步是定义任务在后台处理的内容。就我而言,我需要填充 mStuff。在您的任务类中,定义 doInBackground() 或 doCheckedInBackground() (如果您想捕获 doChecked... 可以抛出异常)。
最后,至少您需要对结果执行一些操作,例如更新类变量或填充 UI 或其他操作。这是在
after
中完成的:正如您提到的,您可以对该类执行更多操作,例如定义一个
before()
覆盖,该覆盖在任务之前在 UI 线程上工作,或failed()
/handleError()
来处理未检查/检查的失败。这只是一个简单的例子,希望对您有所帮助。Droid-fu is a little outdated now, mainly due to the lack of Fragment support. But I'll give an example from an app I wrote that used it.
First, your activity class has to subclass BetterActivity (or BetterXXXActivity). In my code I was using a ListActivity so mine here subclasses BetterListActivity. I also define a subclass of BetterAsyncTask so I can extend some functionality.
Now, my task needs doesn't take a parameter, uses an indeterminate dialog so no progress is published, and needs to return an Integer. Your needs may differ, and that affects the types used in the class definition.
Next step is to define what the task processes in the background. In my case I need to populate mStuff. In your task class, define either doInBackground() or doCheckedInBackground() (doChecked... can throw an exception if you want to catch it).
Finally, at the very least you need to do something with your result, like update a class variable or populate the UI or something. This is done in
after
:As you mentioned there's more you can do with the class, such as define a
before()
override that does work on the UI thread before the task, orfailed()
/handleError()
to handle unchecked/checked failures. This is just a simple example, hope it helps.@ss 多么痛苦啊!该错误很好地隐藏在 BetterActivityHelper 类中:
progressDialogTitleId 和 ProgressDialogMsgId 需要 res/values/string.xml 中的值:
如果未定义它们,则会引发运行时异常。
不幸的是,如果未记录,即使是最好的辅助类也几乎毫无用处。我花了几个小时才弄清楚出了什么问题。再次向开发者表示不“谢谢”。索尔
What a pain in the @ss! The error was well hidden inside the BetterActivityHelper class:
progressDialogTitleId and progressDialogMsgId expect a value inside the res/values/string.xml :
If they are not defined, a runtime exception will be thrown.
Unfortunately, even the best helper classes are almost useless if they are UNDOCUMENTED. Took me a couple of hours to figure out what went wrong. So again, NO "Thank you" to the developer. Sor