First of all... separate the updating logic from your onCreate
method. So, for instance, you can create an updateHTML()
.
Then, you can use a Timer
in order to update the page periodically:
public class YourActivity extends Activity {
private Timer autoUpdate;
public void onCreate(Bundle b){
super.onCreate(b);
// whatever you have here
}
@Override
public void onResume() {
super.onResume();
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
updateHTML();
}
});
}
}, 0, 40000); // updates each 40 secs
}
private void updateHTML(){
// your logic here
}
@Override
public void onPause() {
autoUpdate.cancel();
super.onPause();
}
}
Notice that I'm canceling the updating task on onPause
, and that in this case the updateHTML
method is executed each 40 secs (40000 milliseconds). Also, make sure you import these two classes: java.util.Timer
and java.util.TimerTask
.