用BetterAsyncTask(Droid-Fu)弥补AsyncTask的缺点

发布于 2024-12-20 07:25:14 字数 1922 浏览 0 评论 0原文

在 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 文件,请告诉我,我可以给您发送一份)

编辑:

可以在这里找到一个很好的例子!以及此处

好的,这是我在源代码中找到的一些附加信息:

  1. 自动显示进度对话框。请参阅 useCustomDialog()、disableDialog()
  2. 如果从 doInBackground 内部抛出异常,则现在由 handleError 方法处理。
  3. 现在您应该不再重写 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:

  1. A progress dialog is automatically shown. See useCustomDialog(), disableDialog()
  2. If an Exception is thrown from inside doInBackground, this is now handled by the handleError method.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-12-27 07:25:14

Droid-fu 现在有点过时了,主要是缺乏 Fragment 支持。但我会举一个我编写的使用它的应用程序的例子。

首先,您的活动类必须是 BetterActivity (或 BetterXXXActivity)的子类。在我的代码中,我使用了 ListActivity,因此我在这里创建了 BetterListActivity 的子类。我还定义了 BetterAsyncTask 的子类,以便可以扩展一些功能。

public class DroidFuExample extends BetterListActivity {

    private ExampleTask mTask;
    private List<Stuff> mMainStuff;

    private class ExampleTask extends BetterAsyncTask<Void, Void, Integer> {
        private List<Stuff> mStuff;
        private DroidFuExample mContext; // a context for lifecycle management
        ...
    }
}

现在,我的任务需要不带参数,使用不确定的对话框,因此不会发布进度,并且需要返回一个整数。您的需求可能有所不同,这会影响类定义中使用的类型。

下一步是定义任务在后台处理的内容。就我而言,我需要填充 mStuff。在您的任务类中,定义 doInBackground() 或 doCheckedInBackground() (如果您想捕获 doChecked... 可以抛出异常)。

    protected Integer doCheckedInBackground(Context context, Void... params)
            throws Exception {
        mStuff = // some long-running code (no longer on the UI thread)
        return 1;
    }

最后,至少您需要对结果执行一些操作,例如更新类变量或填充 UI 或其他操作。这是在 after 中完成的:

    protected void after(Context context, Integer integer) {
        if (integer >= someAcceptablePositiveConstant) {
            mMainStuff = mStuff;
            doSomethingInTheUIWithMainStuff();
        } else {
            //gah!
        }
    }

正如您提到的,您可以对该类执行更多操作,例如定义一个 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.

public class DroidFuExample extends BetterListActivity {

    private ExampleTask mTask;
    private List<Stuff> mMainStuff;

    private class ExampleTask extends BetterAsyncTask<Void, Void, Integer> {
        private List<Stuff> mStuff;
        private DroidFuExample mContext; // a context for lifecycle management
        ...
    }
}

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

    protected Integer doCheckedInBackground(Context context, Void... params)
            throws Exception {
        mStuff = // some long-running code (no longer on the UI thread)
        return 1;
    }

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:

    protected void after(Context context, Integer integer) {
        if (integer >= someAcceptablePositiveConstant) {
            mMainStuff = mStuff;
            doSomethingInTheUIWithMainStuff();
        } else {
            //gah!
        }
    }

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, or failed() / handleError() to handle unchecked/checked failures. This is just a simple example, hope it helps.

笑看君怀她人 2024-12-27 07:25:14

@ss 多么痛苦啊!该错误很好地隐藏在 BetterActivityHelper 类中:

public static ProgressDialog createProgressDialog(final Activity activity,
        int progressDialogTitleId, int progressDialogMsgId) {
    ProgressDialog progressDialog = new ProgressDialog(activity);
    if (progressDialogTitleId <= 0) {
        progressDialogTitleId = activity.getResources().getIdentifier(
                PROGRESS_DIALOG_TITLE_RESOURCE, "string", activity.getPackageName());
    }
    progressDialog.setTitle(progressDialogTitleId);
    if (progressDialogMsgId <= 0) {
        progressDialogMsgId = activity.getResources().getIdentifier(
                PROGRESS_DIALOG_MESSAGE_RESOURCE, "string", activity.getPackageName());
    }
    progressDialog.setMessage(activity.getString(progressDialogMsgId));
    progressDialog.setIndeterminate(true);
    progressDialog.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            activity.onKeyDown(keyCode, event);
            return false;
        }
    });

progressDialogTitleId 和 ProgressDialogMsgId 需要 res/values/string.xml 中的值:

<!-- Droid-Fu Progressdialog -->
<string name="droidfu_progress_dialog_title">Some nasty dialog title</string>
<string name="droidfu_progress_dialog_message">Some funny message</string>

如果未定义它们,则会引发运行时异常。

不幸的是,如果未记录,即使是最好的辅助类也几乎毫无用处。我花了几个小时才弄清楚出了什么问题。再次向开发者表示“谢谢”。索尔

What a pain in the @ss! The error was well hidden inside the BetterActivityHelper class:

public static ProgressDialog createProgressDialog(final Activity activity,
        int progressDialogTitleId, int progressDialogMsgId) {
    ProgressDialog progressDialog = new ProgressDialog(activity);
    if (progressDialogTitleId <= 0) {
        progressDialogTitleId = activity.getResources().getIdentifier(
                PROGRESS_DIALOG_TITLE_RESOURCE, "string", activity.getPackageName());
    }
    progressDialog.setTitle(progressDialogTitleId);
    if (progressDialogMsgId <= 0) {
        progressDialogMsgId = activity.getResources().getIdentifier(
                PROGRESS_DIALOG_MESSAGE_RESOURCE, "string", activity.getPackageName());
    }
    progressDialog.setMessage(activity.getString(progressDialogMsgId));
    progressDialog.setIndeterminate(true);
    progressDialog.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            activity.onKeyDown(keyCode, event);
            return false;
        }
    });

progressDialogTitleId and progressDialogMsgId expect a value inside the res/values/string.xml :

<!-- Droid-Fu Progressdialog -->
<string name="droidfu_progress_dialog_title">Some nasty dialog title</string>
<string name="droidfu_progress_dialog_message">Some funny message</string>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文