扩展AsyncTask的<虚空,虚空,虚空>虚空、AsyncTask、LT、GT

2023-09-05 23:33:32 作者:糖果味的风

在我的Andr​​oid应用程序,我通过延长执行一些操作中的 doInBackground 的AsyncTask<虚空,虚空,虚空> 类。 (我有此一等级执行任何UI没有用的)

In my android app I am performing some operations in the doInBackground by extending AsyncTask<Void, Void, Void> class. (I have no use in performing any UI in this class)

这是正确使用的AsyncTask的? 如果这样我可以扩展AsyncTask的呢? 扩展之间的区别是什么的AsyncTask 的AsyncTask&LT;虚空,虚空,虚空&GT; Is this proper use of AsyncTask ? If so can I extend AsyncTask instead ? What is the difference between extending AsyncTask and AsyncTask<Void, Void, Void>

code例如:

public class MessagePooling extends AsyncTask<Void, Void, Void>
{        
    @Override
    protected Void doInBackground(Void... params) 
    {
        while (!isCancelled()) 
        {           
           //Getting data from server            
            SystemClock.sleep(1000);
        }
        return null;
    }
}

或者

public class MessagePooling extends AsyncTask
{
    @Override
    protected Object doInBackground(Object... params) 
    {
        while (!isCancelled()) 
        {           
           //Getting data from server            
            SystemClock.sleep(1000);
        }
        return null;    
    }
}

感谢

推荐答案

的AsyncTask 类可以被认为是一个非常方便的线程机制。它为您提供了一些工具,你可以使用简单的Java线程根本没有如取消清理操作。你不必做任何用户界面的背景。

The AsyncTask class can be thought of as a very convenient threading mechanism. It gives you a few tools that you can use that simple Java threads simply don't have such as on cancel cleanup operations. You don't have to do any UI in the background. You could simply execute one by writing one as an anonymous class like this:

    new AsyncTask<Integer, Void, Void>(){
        @Override
        protected Void doInBackground(Integer... params) {
            // **Code**
            return null;
        }
    }.execute(1, 2, 3, 4, 5);

这将执行不管你放在 doInBackground 与给定参数的后台线程。同样的,你可以简单地使用虚空和不带参数运行。

It will execute whatever you put in doInBackground on a background thread with the given parameters. Likewise, you can simply use Void and execute with no parameters.

我能想到的执行线程这种方式唯一的好处是在日后的维护提供帮助。有可能是要修改都必须在UI线程上的某些东西,你会在这种情况下,覆盖等方法的情况。其他的情况将是你根本就没有做的动作足以证明写出另一个类,因此只需创建一个在飞行,并用它做。

The only advantage I could think of executing a thread this way would be to aid in future maintenance. There might be a case where you want to modify certain things that are required to be on the UI thread, in which case you would override the other methods. Other cases would be you simply don't do the action enough to justify writing out another class, so just create one on the fly and be done with it.

编辑:

要回答#3:他们实际上是相同的。该虚空对象是一个Java对象,就像其他任何东西。你不使用虚空,让你在它使用的什么地方都无所谓。这只是的AsyncTask 合同要求要传递的三级类型,默认情况下他们是对象这是基准类别的一切。

To answer #3: they're effectively the same. The Void object is a Java object just like anything else. You're not using Void, so what you use in it's place doesn't matter. It's just the AsyncTask contract requires three class types to be passed in, and by default they're Object which is the baseline class of everything.