在 WebView 上设置 loadURLTImeOutValue [英] Set loadURLTImeOutValue on WebView

查看:16
本文介绍了在 WebView 上设置 loadURLTImeOutValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PhoneGap 和 Android,并且在外部服务器上有我的 .html 和 js 文件.当我使用以下代码时,应用程序会加载我的外部 .html 文件并且一切正常:

I'm working with PhoneGap and Android and have my .html and js files on an external server. When I use the following code, the app loads my external .html files and everything works fine:

this.setIntegerProperty("loadUrlTimeoutValue", 60000);
this.loadUrl("http://www.myserver.com");

但是,当通过 WebView 工作时,我似乎无法为 WebView 设置 loadURLTimeoutValue:

However, when work via a WebView I can't seem to set the loadURLTimeoutValue for a WebView:

private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);  

try {
     webView = (WebView) findViewById(R.id.webview);    
     webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
     webView.loadUrl("http://www.myserver.com");     
}

这不起作用.如何在 WebView 上设置超时值?

This doesn't work. How can I set the timeout value on the WebView?

推荐答案

这是一种模拟所描述行为的解决方法.您可以使用 WebViewClient,并覆盖 onPageStarted 方法:

This is a workaround to simulate the described behavior. You can use a WebViewClient, and override the onPageStarted method:

public class MyWebViewClient extends WebViewClient {
    boolean timeout;

    public MyWebViewClient() {
        timeout = true;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                timeout = true;

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(timeout) {
                    // do what you want
                }
            }
        }).start();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        timeout = false;
    }
}

如果超时,您可以加载,例如,错误页面...

If timeout, you can load, for example, an error page...

要将 WebViewClient 添加到您的 WebView,只需执行以下操作:

To add the WebViewClient to you WebView, just do this:

webView.setWebViewClient(new MyWebViewClient());

这篇关于在 WebView 上设置 loadURLTImeOutValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆