我正在 webview 中加载 url,但如果 wifi 关闭,我将打开 WIFI 设置。
现在我希望当用户打开 wifi 并返回时 webview 应该刷新它自己以再次加载 url。
有没有办法仅在网页由于任何原因未加载时才在 5 秒后连续刷新。
最佳答案
您可以检查 onResume() 方法,如果用户已返回并打开 wifi,然后调用他的方法
@Override
public void onResume() {
super.onResume();
if(connected){
webview.loadUrl("your url");
}
}
要每 5 秒刷新一次,您可以使用计时器
Handler ha=new Handler();
ha.postDelayed(new Runnable() {
@Override
public void run() {
webview.loadUrl("your url");
ha.postDelayed(this, 5000);
}
}, 5000);
我刚刚在 if 条件中编写了连接。您必须使用检查连接的方式。
wv.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
您可以保留一个 boolean 标志来检查 URL 加载是否有错误,并在 onREceived 错误中更改其值。仅当上次有错误时才加载新的 url。
关于java - 当用户打开 wifi 并返回应用程序时刷新 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42453886/