Android webview should overrideUrlLoading and redirect
来源:互联网 发布:windows 8.1 WIFI 编辑:程序博客网 时间:2024/06/04 18:25
Androids WebView class provides a method called shouldOverrideUrlLoading to intercept the loading of the requested URLs.
This gives us the ability to suppress loading of the given URL or handle a URL in the external browser for example.
If you want to prevent the webview from loading the URL you have to return true. Otherwise the url is forwarded to the webview as usual.
- _webView.setWebViewClient(new WebViewClient() {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- boolean shouldOverride = false;
- if (url.startsWith("https://")) { //NON-NLS
- // DO SOMETHING
- shouldOverride = true;
- }
- return shouldOverride;
- }
- }
This mechanism works fine for all URLs triggered by a user tapping on a link.
Unfortunately this method does not get invoked if the URLs source is a redirect on devices running Android < 3.0 (API Level 10 and lower).
Although it will be invoked an works just fine on devices with Android >= 3.0 (API Level 11 and up).
Android < 3.0 -> shouldOverrideUrlLoading will not be called on redirects
Android >= 3.0 -> shouldOverrideUrlLoading will be called even on redirects
You can find some fellow developers facing the same issue.
As a Workaround we use the recommended onPageStarted(WebView view, String url, Bitmap favicon)
Usage is quite the same as shouldOverrideUrlLoading:
- _webView.setWebViewClient(new WebViewClient() {
- @Override
- public void onPageStarted(WebView view, String url, Bitmap favicon){
- if (url.startsWith("https://")) { //NON-NLS
- view.stopLoading();
- // DO SOMETHING
- }
- }
- }
With view.stopLoading
the webview will stop loading of the new URL and still show the current content. This equals the behavior of shouldOverrideUrlLoading returning true.
The advantage is it works on all Android versions.
However the drawback is onPageStarted is invoked AFTER the page was requested form server. That means, the request is already sent to the server even if the response is afterward ignored.
The method shouldOverrideUrlLoading would let you omit the request BEFORE it is sent. So you would be able to save the outgoing web request.
- Android webview should overrideUrlLoading and redirect
- Android WebView blocks redirect from https to http
- android webview and javascript 交互
- android webView setWebChromeClient and setWebClient
- UpdatePanel and Response.Redirect
- Android WebView J Should not happen: no rect-based-test nodes found
- Buttons in button bars should be borderless; use style="?android:attr/buttonBarButtonStyle" (and ?an
- Buttons in button bars should be borderless; use style="?android:attr/buttonBarButtonStyle" (and ?an
- 【Android】【Webview】WebView 简介
- Developers should know How, What and Why
- Table is crashed and should be repaired
- "Calendars" and "DateFormats" should not be static
- webview : Should not happen: no rect-based-test nodes found
- Redirect and POST in ASP.NET
- redirect stdout and back to screen
- Struts 1 Logic 标签 - Forward And Redirect
- Difference between JSP forward and redirect.
- Webview and HTML5 <video>
- java注解应用-java.lang.annotation
- 中国天气网API
- Myeclipse下ext插件spket的配置和安装
- VC++ 实现soap服务端
- MemcacheD、Repcached在centos上的安装步骤
- Android webview should overrideUrlLoading and redirect
- Kettle连接Mysql数据库异常
- 设置eclipse工作区背景色
- 2013-10-12 推荐 江苏吴中【仅供参考,投资需谨慎,自行负责】
- 去除Dialog边框
- 安装ia32-libs
- 数据结构、算法面试系列【1】
- CoCos2D-X-2.1.5中使用Cygwin创建新项目,并在Eclipse中编译通过
- MapReduce中InputFormat和InputSplit解读