3

Possible Duplicate:
Updating TextView every N seconds?

Here, I want to update the Hr value in the textview once it is calculated for every iteration, but with a delay of 2 seconds each time. I dont know how to do it. What i get now in the textview is the last value of the iteration. I want all the values to be displayed at a constant delay. anyone help pls.

    for(int y=1;y<p.length;y++)
    {
       if(p[y]!=0)
        {
        r=p[y]-p[y-1];
          double x= r/500;
          Hr=(int) (60/x);
          Thread.sleep(2000);
         settext(string.valueof(Hr));
      }
    }
Community
  • 1
  • 1
Pooja
  • 77
  • 1
  • 6

5 Answers5

4
public class MainActivity extends Activity{
protected static final long TIME_DELAY = 5000;
//the default update interval for your text, this is in your hand , just run this sample
TextView mTextView;
Handler handler=new Handler();  
int count =0;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView=(TextView)findViewById(R.id.textview);
    handler.post(updateTextRunnable);
}


Runnable updateTextRunnable=new Runnable(){  
  public void run() {  
      count++;
      mTextView.setText("getting called " +count);
      handler.postDelayed(this, TIME_DELAY);  
     }  
 };  
}

I hoped this time you will get into the code and run it .

Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29
  • where does the TIME_DELAY changes? – Pooja Jan 23 '13 at 14:05
  • it's upto you can define the initial offset then it will update your textview after this amount of delay . Suppose u give TIME_DELAY =5000 then your text will update after every 5 sec . u just need to change u r test in mTextView.setText("your updated text goes here" ); – Vipin Sahu Jan 23 '13 at 14:15
  • I dont get it. where should i alter the delay value? pls help – Pooja Jan 23 '13 at 14:48
  • you do not need to alter the Runnable will be get called every 5 sec interval . – Vipin Sahu Jan 25 '13 at 07:09
2

you should use timer class....

Timer timer = new Timer();
        timer.schedule(new TimerTask() {

        public void run() {


        }, 900 * 1000, 900 * 1000);

Above code is for every 15 minutes.change this value and use in your case.....

  • we need to use runOnUiThread() method for updating TextView from TimerTask Thread – ρяσѕρєя K Jan 23 '13 at 09:46
  • Thanks for the answer. Im new to android. I tried this. but, its not working. where am i doing wrong? pls help. for(int y=1;y – Pooja Jan 23 '13 at 10:47
  • you should write 300 instead of 9 for every 5minutes update...cheers –  Jan 23 '13 at 11:18
  • my app gets force closed. so, is the way i have tried is right? and i want the delay for only 5 seconds, how should i do that – Pooja Jan 23 '13 at 14:44
  • Parameters task-->the task to schedule. delay-->amount of time in milliseconds before first execution. period-->amount of time in milliseconds between subsequent executions.so you should write in delay time and period 5*1000 for 5 second –  Jan 24 '13 at 05:36
2

use Handler or TimerTask(with runOnUiThread()) instead of for loop for updating text after every 5 seconds as :

Handler handler=new Handler();  

handler.post(runnable);  
Runnable runnable=new Runnable(){  
  @Override  
    public void run() {  
      settext(string.valueof(Hr));  //<<< update textveiw here
      handler.postDelayed(runnable, 5000);  
     }  
 };  
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

TimerTask is just what you need.

Alpay
  • 1,350
  • 2
  • 24
  • 56
0

lets us just hope it helps you sufficiently

import java.awt.Toolkit;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class demo 
{
  Toolkit toolkit;
  Timer timer;
  public demo()
  {
    toolkit = Toolkit.getDefaultToolkit();
    timer = new Timer();
    timer.schedule(new scheduleDailyTask(), 0, //initial delay
        2 * 1000); //subsequent rate
  }
  class scheduleDailyTask extends TimerTask 
  {
    public void run() 
    {
      System.out.println("this thread runs for every two second");
      System.out.println("you can call this thread to start in your activity");
      System.out.println("I have used a main method to show demo");
      System.out.println("but you should set the text field values here to be updated simultaneouly");
    }
  }
  public static void main(String args[]) {
    new demo();
  }
}
  • I tried this.. It works perfectly as java app. But, in my app i get the error in toolkit = Toolkit.getDefaultToolkit(); when i call the class from my main activity. and my chart activity also gets affected. Is there any way to do this in background asynchronously? – Pooja Jan 23 '13 at 14:51
  • refer http://eliasbland.wordpress.com/2011/03/11/an-example-of-how-to-run-a-background-task-and-report-progress-in-the-status-bar-using-asynctask-on-android/ , this is using the background task , it is not exactly what you need but will get you started , keep me posted for any further help – Hussain Akhtar Wahid 'Ghouri' Jan 23 '13 at 15:10