Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this by using AsyncTask: <a href="http://developer.android.com/reference/android/os/AsyncTask.html" rel="nofollow">http://developer.android.com/reference/android/os/AsyncTask.html</a></p> <p>Do note that in some versions on Android, AsyncTask is executed on a single thread. If you need to execute in paralell this need to be set on the task, from the documentation:</p> <blockquote> <p>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 with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.</p> </blockquote> <p>In addition, you will run into concurrency issues, so you should take steps to handle them.</p> <p>It might look like this:</p> <pre><code>public class DoingTwoThings { private AsyncTask&lt;Void, Void, Void&gt; taskA; private AsyncTask&lt;Void, Void, Void&gt; taskB; private volatile int i; public DoingTwoThings() { createTaskA(); createTaskB(); startTasks(); } private void createTaskA() { taskA = new AsyncTask&lt;Void, Void, Void&gt;() { @Override protected Void doInBackground(Void... voids) { doInBackgroundA(); return null; } }; } private void doInBackgroundA() { while (i &lt; 5000) { i++; } } private void createTaskB() { taskB = new AsyncTask&lt;Void, Void, Void&gt;() { @Override protected Void doInBackground(Void... voids) { doInBackGroundB(); return null; } }; } private void doInBackGroundB() { while (i &gt; 0) { i--; } } private void startTasks() { // AsyncTasks executed one one thread in Honeycomb+ unless executed in thread pool manually if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) { taskA.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); taskB.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { taskA.execute(); taskB.execute(); } }} </code></pre> <p>The code in the overriden "doInBackground()" methods are executed on a different thread. If you need to modify some UI before or after the task is done you can easily override onPreExecute and onPostExecute.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload