2015-04-23 49 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)它會幫助你。

希望它有幫助,

謝謝。

相關問題