If we want to update UI from a Non-UI thread then runOnUiThread() is the best solution for it.
I am showing Toast in every 25 secs.
Below is the code
private Thread refreshThread;
refreshThread = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(25000);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(DriversHomeActivity.this, "Thread", Toast.LENGTH_SHORT).show();
}
});
}
} catch (InterruptedException e) {
}
}
};
refreshThread.start();
When we want to stop an this, just call interrupt() method.
refreshThread.interrupt();
0 Comment(s)