有人可以解释如何在 Android 上使用 ASyncTask 吗? [英] Can somebody explain how to use ASyncTask with Android?

查看:17
本文介绍了有人可以解释如何在 Android 上使用 ASyncTask 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做大量研究并查看 Android 中 ASyncTask 的文档,但我似乎无法理解它.我只是想在片段在应用程序中可见时运行一些方法,但为了更轻松地做到这一点,我认为我应该使用 ASyncTask.我的示例代码如下:

I've been doing a bunch of research and looking over the documentation for ASyncTask in Android, but I just can't seem to wrap my head around it. I simply want to run some methods while a fragment is visible in an application, but in order to more easily do that, I think I should be using ASyncTask. My example code is as follows:

private class syncExample extends ASyncTask <Void, Void, Void>
{
    @Override
    protected void onPreExecute()
    {

    }
    @Override
    protected void doInBackground(Void... voids)
    {

    }
    @Override
    protected void onProgressUpdate()
    {

    }
    @Override
    protected void onPostExecute()
    {

    }
}

现在我的问题如下:

  1. 在尖括号中,我有空,空,空.这些究竟代表什么,我怎么知道在那里放置什么是正确的?

  1. In the angle brackets, I have Void, Void, Void. What exactly do those represent and how do I know what's correct to place in there?

对于类中的每个方法,我将每个方法称为 void.它们什么时候应该与 void 不同(例如 boolean、String、long 等)?

For each method within the class, I have the each method called as void. When should they be different than void (like boolean, String, long, etc.)?

对于 doInBackground 方法,我在括号中有 Void... voids.我到底应该在那里放什么?它们代表什么?

For the doInBackground method, I have Void... voids in the parenthesis. What exactly should I be putting in there? What do they represent?

感谢您的帮助.对于像我这样的初学者来说,这方面的文档不是很清楚.

Thank you for your help. The documentation on this is not very clear for a beginner like myself.

推荐答案

AsyncTask 使用参数化类型(java 泛型),以便您可以在定义自己的 AsyncTask 时指定它使用的类型.也许用这种形式更容易解释:

AsyncTask uses parameterized types (java generics) so that you can specify the types it uses when you define your own AsyncTask. Perhaps it's easier to explain in this form:

public abstract class AsyncTask<Params, Progress, Result> {
    ...
    protected abstract Result doInBackground(Params... params);

    protected abstract void onProgressUpdate(Progress... progress);

    protected abstract void onPostExecute(Result result);
    ...
}

没有名为 Params、Progress 或 Result 的类.这些是泛型类型.它们只是您在定义自己的 AsyncTask 子类时希望使用的类型的占位符.上面同样可以写成这样:

There are no classes named Params, Progress, or Result. These are instead generic types. They are just placeholders for types you wish to use when you define your own AsyncTask subclass. The above could equally be written as such:

public abstract class AsyncTask<A, B, C> {
    ...
    protected abstract C doInBackground(A... params);

    protected abstract void onProgressUpdate(B... progress);

    protected abstract void onPostExecute(C result);
    ...
}

假设我定义了一个 AsyncTask,它接受一个表示 URL 的字符串列表,它将 ping 每个字符串以查看它是否可访问,然后返回可访问的数字.同时,对于每个测试,它会在每个测试完成时更新 ProgressBar.它可能看起来像这样:

Suppose I were defining an AsyncTask that takes a list of Strings representing URLs, and it will ping each one to see if it's reachable, then return the number that were reachable. Meanwhile, with each test, it will update a ProgressBar as each test completes. It might look something like this:

public class MyAsyncTask extends AsyncTask<String, Integer, Integer> {

    @Override
    protected Integer doInBackground(String... params) {
        int total = params.length;
        int successfulPings = 0;
        for (int i = 0; i < total; i++) {
            if (isReachable(params[i])) {
                successfulPings++;
            }
            publishProgress(i, total);
        }
        return successfulPings;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        int testsSoFar = progress[0];
        int totalTests = progress[1];
        progressBar.setMax(totalTests);
        progressBar.setProgress(testSoFar);
    }

    @Override    
    protected void onPostExecute(Integer result) {
        Toast.makeTest(context, "Reached " + result + " sites.", Toast.LENGTH_SHORT).show();
    }
}

我会这样启动:

String[] urls = ...
MyAsyncTask task = new MyAsyncTask();
task.execute(urls);

传入execute 的参数将传入doInBackground.无论您在doInBackground 中做什么,您都需要返回一些作为参数传入onPostExecute 的内容.而在 doInBackground 中,您可以调用 publishProgress,在那里您可以像我一样做一些事情(但您不必这样做).

The argument passed into execute will be passed into doInBackground. Whatever you do in doInBackground, you need to return something that gets passed in as the argument to onPostExecute. While in doInBackground, you can call publishProgress, where you can do something like I did (but you don't have to).

这篇关于有人可以解释如何在 Android 上使用 ASyncTask 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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