Whether a timer can be set to refresh the webview every 1 min only if the application is currently active?
Whether it's possible?
First of all, you need to create a TimerTask
class:
protected class ReloadWebView extends TimerTask {
Activity context;
Timer timer;
WebView wv;
public ReloadWebView(Activity context, int seconds, WebView wv) {
this.context = context;
this.wv = wv;
timer = new Timer();
/* execute the first task after seconds */
timer.schedule(this,
seconds * 1000, // initial delay
seconds * 1000); // subsequent rate
/* if you want to execute the first task immediatly */
/*
timer.schedule(this,
0, // initial delay null
seconds * 1000); // subsequent rate
*/
}
@Override
public void run() {
if(context == null || context.isFinishing()) {
// Activity killed
this.cancel();
return;
}
context.runOnUiThread(new Runnable() {
@Override
public void run() {
wv.reload();
}
});
}
}
In your Activity, you can use this line:
new ReloadWebView(this, 60, wv);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With