0

I have developed an android application which extracts single line text messages from the server. Once a button is clicked, it makes a function call which gets the next message from the server. Some of those messages are time based,

i.e those messages have to be displayed in the TextView for a particular amount of time and after that time is elapsed, it should automatically make the function call to get the next message from the server(i.e without the button being clicked).

Could someone please help me out in achieving this.

I tried using while loop as follows:

while(!presentTime.equals(expiryTime)){                 
    calculatePresentTym();   //This method calculates the presentTime value
    display.settext(the received instruction);
}

if(presentTime.equals(expiryTime))
    (make the function call)

If I do this, nothing is being displayed till presentTime and expiryTime are equal. Once they are equal, the next instruction is automatically fetched by the function call and is displayed in the TextView.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
Pratheek
  • 15
  • 1
  • 6

2 Answers2

1

Use a a handler

Handler m_handler;
Runnable m_handlerTask ; 
m_handler = new Handler(); 
@Override
public void run() {
           // do something 
  m_handler.postDelayed(m_handlerTask, 1000);
 }
 };
 m_handlerTask.run(); 

T0 cancel the run

 m_handler.removeCallbacks(m_handlerTask);  // to cancel the run

You can also use a timer but you will have to use runOnUiThread to update ui since timer runs on a different thread.

Timer _t = new Timer();  
 _t.scheduleAtFixedRate( new TimerTask() {
        @Override
        public void run() {
          //do something
           runOnUiThread(new Runnable() //run on ui thread
             {
              public void run() 
              {      
                //update ui
              }
             });

        }
    }, 1000, 1000 ); 

Note:

gets the next message from the server

Getting the message from server should be done on a background thread.

Edit:

While copy pasting the initialization part was missing. You have a counter i that is displayed in the textview. The counter increases by 1 every second. When it reaches 100 you cancel the run. Modify the below according to your requirements.

 public class MainActivity extends Activity {

TextView tv;
Handler m_handler;
Runnable m_handlerTask ; 
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textView1);
     m_handler = new Handler();
    m_handlerTask = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if(i<=100)
            {
            tv.setText(""+i);
             i++;
            }
            else
            {
                m_handler.removeCallbacks(m_handlerTask);
            }
            m_handler.postDelayed(m_handlerTask, 1000);
        }
    };
    m_handlerTask.run();  
   }
   }  
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thanks Raghunandan. Will try that and get back. – Pratheek Jul 11 '13 at 04:01
  • I implemented the first logic.. It says "m_handlerTask has not been initialized and it suggests to initialize it to null" I do so and then there are yellow lines below run() and m_handlerTask.run(). And when I run the application, an exception is being generated in this line - "m_handlerTask.run();" Any idea what's happening? :( – Pratheek Jul 11 '13 at 05:00
  • @Pratheek you will have to initialize the `m_Handler` – Raghunandan Jul 11 '13 at 05:26
  • Yeah it worked partially.. As in, the counter thing which u showed was perfect. But I want to display some text continuously for about 60 seconds. I replace the if statement as I require. Now after 60 seconds I want to automatically call the onCreate() method. But that's not happening. The same text is being displayed in the textview even after 60 seconds. Where should I place the function call for onCreate? – Pratheek Jul 11 '13 at 06:51
  • @Pratheek i am not sure what you want. rethink your design. `oncreate` is called once during the life time of an activity. read the activity lifecycle docs – Raghunandan Jul 11 '13 at 06:52
  • I've got another doubt. Where does the control go after 100 seconds in your code? – Pratheek Jul 11 '13 at 06:53
  • @Pratheek the handler stops its run. What do you mean by control. the execution flow is sequential. – Raghunandan Jul 11 '13 at 06:54
  • Sorry for the trouble.. I want to call onCreate() again because the function call for extracting the next message from the server is inside onCreate() of the present activity itself. – Pratheek Jul 11 '13 at 06:55
  • @Pratheek pls read the activity lifecycle. `oncreate` is called only once during the lifecycle of an activity. if its destroyed then you `oncreate` will get called again – Raghunandan Jul 11 '13 at 06:59
0

Use a timer. Schedule the timer for repeated interval executions, and after each execution you can get the next text from the server and display the same.

Check the Timer reference scheduleAtFixedRate(TimerTask task, long delay, long period)

Rajeev
  • 1,374
  • 1
  • 11
  • 18