I have to display some data after every 10 seconds. Can anyone tell me how to do that?
-
1@Farha, I hope that my answer was helpful. – Kiril Mar 29 '10 at 21:22
6 Answers
There is an another way also that you can use to update the UI on specific time interval. Above two options are correct but depends on the situation you can use alternate ways to update the UI on specific time interval.
First declare one global varialbe for Handler to update the UI control from Thread, like below
Handler mHandler = new Handler();
Now create one Thread and use while loop to periodically perform the task using the sleep method of the thread.
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();

- 23,293
- 19
- 66
- 73

- 1,198
- 7
- 5
-
1Cannot refer to a non-final variable mHandler inside an inner class defined in a different method – Francisco Corrales Morales Nov 21 '14 at 15:55
-
Probably the simplest thing to do is this:
while(needToDisplayData)
{
displayData(); // display the data
Thread.sleep(10000); // sleep for 10 seconds
}
Alternately you can use a Timer:
int delay = 1000; // delay for 1 sec.
int period = 10000; // repeat every 10 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
displayData(); // display the data
}
}, delay, period);

- 39,672
- 31
- 167
- 226
-
2Lirik, your first solution would throw an ANR is definitely not advisable. And it would have been nicer if you mentioned if timer.scheduleAtFixedRate would create a new thread for the task to be performed or use the same thread it was called on. – tony9099 Sep 19 '13 at 11:32
-
1first one freezes ui, second one throws ex: `java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()` – lxknvlk Mar 21 '15 at 22:44
There is Also Another way by Using Handler
final int intervalTime = 10000; // 10 sec
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Display Data here
}
}, intervalTime);

- 6,331
- 4
- 51
- 81

- 111
- 1
- 1
Andrahu was on the right track with defining a handler. If you have a handler that calls your update functions you can simply delay the message sent to the handler for 10 seconds.
In this way you don't need to start your own thread or something like that that will lead to strange errors, debugging and maintenance problems.
Just call:
Handler myHandler = new MyUpdateHandler(GUI to refresh); <- You need to define a own handler that simply calls a update function on your gui.
myHandler.sendMessageDelayed(message, 10000);
Now your handleMessage function will be called after 10 seconds. You could just send another message in your update function causing the whole cycle to run over and over

- 187,060
- 113
- 301
- 369
There is a Simple way to display some data after every 10 seconds.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
ActionStartsHere();
}
public void ActionStartsHere() {
againStartGPSAndSendFile();
}
public void againStartGPSAndSendFile() {
new CountDownTimer(11000,10000) {
@Override
public void onTick(long millisUntilFinished) {
// Display Data by Every Ten Second
}
@Override
public void onFinish() {
ActionStartsHere();
}
}.start();
}

- 2,037
- 9
- 42
- 54

- 111
- 1
- 1
Every 10 seconds automatically refreshed your app screen or activity refreshed
create inside onCreate() method i tried this code will work for me
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
//CALL ANY METHOD OR ANY URL OR FUNCTION or any view
}
});
}
} catch (InterruptedException e) {
}
}
};t.start();

- 1
- 1

- 496
- 1
- 7
- 14
-
this is not correct way check my ans here your ui thread is wait and that’s why may chances your app goes to not responding mode – Yogesh Rathi Apr 16 '16 at 07:08
-
i want to automatically re call url and method or any thing how can i do – Satheeshkumar Somu Mar 03 '17 at 12:22