2015-04-23 46 views
0

比如有一个字符串的AsyncTask ... parameters,如果我做这样的呼吁:AsyncTask是否按照其每个参数顺序或随机地运行doInBackground?

AsyncTask<String, Void, Void> someTask = new myTask(myActivity.this); 
someTask.execute(string1 , string2 , string3); 

这是什么任务里面的doInBackground执行的内部顺序:它把字符串1首然后是string2等等,因为它们是在被调用时提供的,还是它随机地处理parameters

+0

[Go through this](http://stackoverflow.com/questions/6053602/what-arguments-are-passed-into-asynctaskarg1-arg2-arg3) – nobalG

+0

它会按照您编写代码的顺序来对待它。它只是将所有字符串压缩成一个String []数据,然后您可以以任何您想要的方式使用它。 –

+0

你想一个接一个地调用'AsyncTask'来获取参数吗? – Paritosh

回答

0

String...是一个“vararg”,在本例中它将所有单个参数转换为String[],其中数组的条目按照它们传入方法的顺序。

因此,使用你的榜样,(字符串[])param[0] == string1param[1] == string2param[2] == string3等等。这是用于param条目的排序,至于如何使用param中的每个条目,它完全取决于您的代码。

2

首先,参数不会随机传递。 This answer会向您解释更多关于参数的信息。同时检查图像从this answer。为了您的理解,我在此添加相同的图像。

enter image description here

1

这可能是在一个线程或并行串口,它实际上取决于其Android操作系统的版本的应用程序正在运行。对于大多数情况下,它将在一个后台线程上串行。

这是谷歌文件说什么: -

Executes the task with the specified parameters. The task returns itself (this) so that the caller can keep a reference to it. 

Note: this function schedules the task on a queue for a single background thread or pool of threads depending on the platform version. When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting HONEYCOMB, tasks are back to being executed on a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can use the executeOnExecutor(Executor, Params...) version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use. 

This method must be invoked on the UI thread. 

检查此链接execute (Params... params)它会帮助你。

希望它有帮助,

谢谢。

相关问题