在Android Studio中为shouldOverrideUrlLoading添加例外?

时间:2016-01-27 12:42:05

标签: android webview

我一直在努力解决这个问题,但到目前为止没有任何运气......所以基本上,我已经有了我的标准webview应用程序,我称之为shouldOverrideUrlLoading。这是一段代码

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
}

但我想为此添加一个例外。例外情况应该是:如果url不包含“scheppersonline”,请不要在webview中打开,而是在标准浏览器应用程序中打开。我试着这样做:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.contains("scheppersonline.be")) {
        view.loadUrl(url);
        return true;
    } else {
        return false;
    }
}

但这似乎不起作用。我刚刚开始编码,所以任何帮助将不胜感激! :)

logcat的:

01-27 13:51:01.406 28229-28229/com.vanbilsen.bram.scheppersmechelen W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 28229
01-27 13:51:01.422 28229-28229/com.vanbilsen.bram.scheppersmechelen I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'setAttribute' of null", source:  (1)
01-27 13:51:02.238 28229-28229/com.vanbilsen.bram.scheppersmechelen W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 28229
01-27 13:51:03.894 28229-28229/com.vanbilsen.bram.scheppersmechelen W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 28229
01-27 13:51:04.064 28229-28229/com.vanbilsen.bram.scheppersmechelen I/chromium: [INFO:CONSOLE(129)] "Uncaught TypeError: Cannot read property 'addEventListener' of null", source: http://scheppersonline.be/wp-content/themes/accesspress-staple/js/main-menu.js?ver=1.0 (129)
01-27 13:51:08.917 28229-28229/com.vanbilsen.bram.scheppersmechelen D/cr_Ime: [ImeAdapter.java:571] focusedNodeChanged
01-27 13:51:08.935 28229-28229/com.vanbilsen.bram.scheppersmechelen D/cr_Ime: [ImeAdapter.java:213] updateKeyboardVisibility: type [0], flags [0], show [true]
01-27 13:51:08.936 28229-28229/com.vanbilsen.bram.scheppersmechelen D/cr_Ime: [AdapterInputConnection.java:178] updateState [] [0 0] [-1 -1] [true]
01-27 13:51:09.842 28229-28229/com.vanbilsen.bram.scheppersmechelen W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 28229
01-27 13:51:25.195 28229-28229/com.vanbilsen.bram.scheppersmechelen D/cr_Ime: [ImeAdapter.java:213] updateKeyboardVisibility: type [0], flags [0], show [true]
01-27 13:51:25.195 28229-28229/com.vanbilsen.bram.scheppersmechelen D/cr_Ime: [AdapterInputConnection.java:178] updateState [] [0 0] [-1 -1] [true]
01-27 13:51:26.781 28229-28229/com.vanbilsen.bram.scheppersmechelen W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 28229

1 个答案:

答案 0 :(得分:0)

如果主机应用程序想要离开当前的WebView并处理url本身,则shouldOverrideUrlLoading返回的布尔值为True,否则为false

澄清......当你返回false时,你并不是说不加载网址。实际上你说好了让WebView负责加载网址。

你真正想要的是:

public boolean shouldOverrideUrlLoading(WebView view, String url) {

    if (!url.contains("scheppersonline.be")) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);
        return true;

    } else {

        return false;
    }
}