有人可以解释如何使用的AsyncTask与Android? [英] Can somebody explain how to use ASyncTask with Android?

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

问题描述

我已经做了一堆的研究和寻找过的AsyncTask的Andr​​oid中的文件,但我似乎无法环绕它我的头。我只是想运行一些方法,而片段中可见的应用程序,但为了更容易地做到这一点,我想我应该使用AsyncTask的。我的例子code是如下:

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. 在尖括号,我有虚空,虚空,虚空。究竟做那些重present?我怎么知道什么是正确的在那里的地方?

  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每个方法。他们什么时候应该比无效不同(如布尔值,字符串,长的,等等)?

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方法,我有虚空......空隙括号。究竟我应该把在那里?他们是怎么重新present?

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,进展或结果的类。这些是不是泛型类型。他们只是占位符,你想,当你定义自己的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,它利用字符串重新presenting的URL列表,它会互相ping通一个看它是否到达,然后返回那名访问的次数。同时,随着每个测试,它会更新一个进度每个测试完成。它看起来是这样的:

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();
    }
}

我会开始具体步骤如下:

I would initiate this as follows:

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

传入参数执行将被传递到 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).

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

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