Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid update Ui from Thread - let another Task join?
    text
    copied!<p>hello everyone out there,</p> <p>atm i am facing a little problem with my current application. As you can see in the following code example i call the shortTask Thread at the end of the longTask Thread. But this is not working as desired. At this point the ui is not updated every 2000 ms as intented - changes are done all at once at the end of the task. </p> <p>So right now i am kindof lost - i may be doing something wrong or missing something - i hope you can give me advice where to go next.</p> <pre><code>public class ThreadActivity extends Activity{ private final Handler myHandler = new Handler(); private Button shortTaskRun; private Button longTaskRun; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); shortTaskRun = (Button)findViewById(R.id.but1); shortTaskRun.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0){ Thread t = new Thread(shortTask); t.start(); } }); longTaskRun = (Button)findViewById(R.id.but2); longTaskRun.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0){ Thread t = new Thread(longTask); t.start(); } }); } /********************* TIMER TASKS *************************/ final TimerTask shortTask = new TimerTask(){ @Override public void run(){ try{ myHandler.post(calcAnim1); myHandler.post(updateUi); // make the changes in the ui visible for 2000 ms Thread.sleep(2000); myHandler.post(calcAnim2); myHandler.post(updateUi); // and again Thread.sleep(2000); // and so on }catch(InterruptedException e){e.printStackTrace();} } }; final TimerTask longTask = new TimerTask(){ @Override public void run(){ try{ while(true){ myHandler.post(calcAnim1); myHandler.post(updateUi); // make the changes in the ui visible for 2000 ms Thread.sleep(2000); if(somecondition) break; myHandler.post(calcAnim2); myHandler.post(updateUi); // and again Thread.sleep(2000); if(somecondition) break; } // AT THIS POINT: i need to start the shortTask myHandler.post(shortTask); }catch(InterruptedException e){e.printStackTrace();} } }; /********************* RUNNABLES ***************************/ final Runnable calcAnim1 = new Runnable(){ // calculation 1 and ui animation }; final Runnable calcAnim2 = new Runnable(){ // calculation 2 and ui animation }; final Runnable updateUi = new Runnable(){ // update views in ui }; } </code></pre> <p><strong>EDIT</strong></p> <p>thx for the help! - as adviced i picked AsyncTask and did following changes:</p> <pre><code>public class ThreadActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); longTaskRun = (Button)findViewById(R.id.but2); longTaskRun.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0){ new LongOperation().execute(""); } }); } /********************* ADDED ASYNC TASK ********************/ private class LongOperation extends AsyncTask&lt;String, Void, String&gt;{ @Override protected String doInBackground(String... arg0){ // longTask code here return "Executed"; } @Override protected void onPostExecute(String result){ // shortTask code here } } } </code></pre>
 

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