是否可以将计时器设置为仅在应用程序当前处于活动状态时每1分钟刷新一次webview?
是否有可能?
答案 0 :(得分:9)
首先,您需要创建一个TimerTask
类:
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();
}
});
}
}
在您的活动中,您可以使用以下行:
new ReloadWebView(this, 60, wv);