当用户打开 wifi 并返回应用程序时刷新 webview

Refresh webview when user open wifi and back to app

我正在网络视图中加载 url,但如果 wifi 关闭,我正在打开 WIFI 设置。

现在我希望当用户打开 wifi 并返回时,webview 应该自行刷新以再次加载 url。

有没有办法在网页因任何原因未加载时,在 5 秒后持续刷新它。

如果用户返回时打开了 wifi,您可以检查 onResume() 方法,然后调用他的方法

@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 condition.You 中写了 connected 将不得不使用您检查连接的方式。

wv.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
    }
 });

您可以保留一个布尔标志来检查 url 加载是否有错误并在 onREceived 错误中更改其值。并加载新的 url 只有在上一次出错时才加载。