How to update interface with regular intervals? [50% solved]

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to update interface with regular intervals? [50% solved]

Postby sandis84 » Thu Sep 10, 2009 9:20 am

Hi, I have a very simple sample code where I try to change a TextView every 5 seconds. It does not work. I guess it is because TimerTask is another thread that does not have the right to alter the Views of the Activity? But no exception is thrown. What is the best solution to my problem?

Here is the code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package Test.TestArea;
  2.  
  3.  
  4.  
  5. import java.util.Timer;
  6.  
  7. import java.util.TimerTask;
  8.  
  9. import android.app.Activity;
  10.  
  11. import android.os.Bundle;
  12.  
  13. import android.util.Log;
  14.  
  15. import android.widget.TextView;
  16.  
  17.  
  18.  
  19. public class TestArea extends Activity {
  20.  
  21.  
  22.  
  23.         TextView text;
  24.  
  25.         private Timer updateTimer;
  26.  
  27.  
  28.  
  29.         /** Called when the activity is first created. */
  30.  
  31.         @Override
  32.  
  33.         public void onCreate(Bundle savedInstanceState) {
  34.  
  35.                 super.onCreate(savedInstanceState);
  36.  
  37.                 setContentView(R.layout.main);
  38.  
  39.  
  40.  
  41.                 text = (TextView) findViewById(R.id.text);
  42.  
  43.                 updateTimer = new Timer("name");
  44.  
  45.                 updateTimer.scheduleAtFixedRate(doRefresh, 5000, 3000);
  46.  
  47.  
  48.  
  49.         }
  50.  
  51.  
  52.  
  53.         private TimerTask doRefresh = new TimerTask() {
  54.  
  55.                 public void run() {
  56.  
  57.                         Log.d("TestArea", "changing text");
  58.  
  59.                         text.setText("lolz");
  60.  
  61.                 }
  62.  
  63.         };
  64.  
  65. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


edit: answer in 5th post
Last edited by sandis84 on Thu Sep 10, 2009 10:23 am, edited 1 time in total.
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Top

Re: How to update interface with regular intervals?

Postby saamyandroid » Thu Sep 10, 2009 9:38 am

You can use Thread to solve this issue
saamyandroid
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 09, 2009 8:17 pm
Location: INDIA

Re: How to update interface with regular intervals?

Postby sandis84 » Thu Sep 10, 2009 9:50 am

saamyandroid wrote:You can use Thread to solve this issue


But another Thread is not allowed to modify the views of the activity?
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby sandis84 » Thu Sep 10, 2009 10:12 am

The problem is further complicated by the fact that my final application should have a socket that I both write to and read from. Thus I cannot start a service either, because I cannot pass a socket to it, right? Perhaps I could just create a thread that reads from the socket, and then sends through a broadcast, and i register a broadcastreceiver that catches it and writes to the activity. This however seems soooo complex for such an easy thing to do...
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby sandis84 » Thu Sep 10, 2009 10:24 am

I solved it. the timertask DO change the text, but I must also call text.refreshDrawableState(); to make the change visible. But is this the best way of solving it?

EDIT: refreshDrawableState solved it for TextViews... however, now I have some problems with showing Toasts. :(

EDIT2: I was mistaking. I have not solved it... refreshDrawableState does not work.

EDIT3: The program even hangs without error messages when I try to toast...
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby sandis84 » Thu Sep 10, 2009 11:36 am

Ok, here we go. This simple code logs "changing text" but not "text changed". It hangs. How can I make it work?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package Test.TestArea;
  2.  
  3.  
  4.  
  5. import java.util.Timer;
  6.  
  7. import java.util.TimerTask;
  8.  
  9. import android.app.Activity;
  10.  
  11. import android.os.Bundle;
  12.  
  13. import android.util.Log;
  14.  
  15. import android.widget.TextView;
  16.  
  17.  
  18.  
  19. public class TestArea extends Activity {
  20.  
  21.  
  22.  
  23.         TextView text;
  24.  
  25.         private Timer updateTimer;
  26.  
  27.  
  28.  
  29.         /** Called when the activity is first created. */
  30.  
  31.         @Override
  32.  
  33.         public void onCreate(Bundle savedInstanceState) {
  34.  
  35.                 super.onCreate(savedInstanceState);
  36.  
  37.                 setContentView(R.layout.main);
  38.  
  39.  
  40.  
  41.                 text = (TextView) findViewById(R.id.text);
  42.  
  43.                 updateTimer = new Timer("name");
  44.  
  45.                 updateTimer.schedule(doRefresh, 1000);
  46.  
  47.  
  48.  
  49.         }
  50.  
  51.  
  52.  
  53.         private TimerTask doRefresh = new TimerTask() {
  54.  
  55.                 public void run() {
  56.  
  57.                         Log.d("TestArea", "changing text");
  58.  
  59.                         text.setText("lolz");
  60.  
  61.                         Log.d("TestArea", "text changed");
  62.  
  63.                 }
  64.  
  65.         };
  66.  
  67.  
  68.  
  69.         @Override
  70.  
  71.         protected void onStop() {
  72.  
  73.                 updateTimer.cancel();
  74.  
  75.                 super.onStop();
  76.  
  77.         }
  78.  
  79. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Top

Postby sandis84 » Thu Sep 10, 2009 12:08 pm

there, now I really did solve it. Here is the solution:

http://www.helloandroid.com/node/243
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby Emmanuel7 » Fri Sep 11, 2009 1:09 am

I think when I was writing this entry on my blog : chaining-two-animations-or-how-to.html, Romain Guy ( a Google Android engineer ) advised me against creating unnecessary handle for this, and to use "runOnUiThread", or "post" for this kind of issues ( modifying the UI from another thread ).

Hope it helps,
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests