【问题标题】:Refresh webview when user open wifi and back to app当用户打开wifi并返回应用程序时刷新webview
【发布时间】:2017-02-25 17:13:34
【问题描述】:

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

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

只有在由于任何原因没有加载网页的情况下,有没有办法在 5 秒后连续刷新它。

【问题讨论】:

  • loadUrl("link") in onResume()

标签: java android webview android-wifi


【解决方案1】:

你可以在 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条件中写了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。

【讨论】:

  • 每 5 秒加载一次,仅当显示无法加载页面时。
猜你喜欢
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多