java - How to display some text in TextView for a specified amount of time? -
i have developed android application extracts single line text messages server. once button clicked, makes function call gets next message server. of messages time based,
i.e messages have displayed in textview
particular amount of time , after time elapsed, should automatically make function call next message server(i.e without button being clicked).
could please me out in achieving this.
i tried using while loop follows:
while(!presenttime.equals(expirytime)){ calculatepresenttym(); //this method calculates presenttime value display.settext(the received instruction); } if(presenttime.equals(expirytime)) (make function call)
if this, nothing being displayed till presenttime
, expirytime
equal. once equal, next instruction automatically fetched function call , displayed in textview.
use a handler
handler m_handler; runnable m_handlertask ; m_handler = new handler(); @override public void run() { // m_handler.postdelayed(m_handlertask, 1000); } }; m_handlertask.run();
t0 cancel run
m_handler.removecallbacks(m_handlertask); // cancel run
you can use timer have use runonuithread
update ui since timer runs on different thread.
timer _t = new timer(); _t.scheduleatfixedrate( new timertask() { @override public void run() { //do runonuithread(new runnable() //run on ui thread { public void run() { //update ui } }); } }, 1000, 1000 );
note:
gets next message server
getting message server should done on background thread.
edit:
while copy pasting initialization part missing. have counter displayed in textview. counter increases 1 every second. when reaches 100 cancel run. modify below according 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(); } }
Comments
Post a Comment