Https加载展示空白页问题
首先给出webview加载https页面出现空白的问题。Denied starting an intent without a user gesture
解决办法如下:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; }}
);
从最新的android,webview和chrome client是分离的应用,可以自动升级。
chrome 版本 >= 25版本,会改变使用webview组件的android应用程序加载url的方式。意图对象被拒绝,解决办法就是:强制启动一个URL 或者在 安卓客户端重写shouldOverrideUrlLoading 这个方法。
From newest androids, the WebView and Chrome Client is separated application which can be automatically updated without user intention.
From Chrome x >= 25 version, they changed how loading url is working in android application which is using webview component. https://developer.chrome.com/multidevice/android/intentsLooks like they are blocking changing url without user gesture and launched from JavaScript timers
Solution here is to force user to activate URL change, for example on button click.
Also, you can override method mentioned above "shouldOverrideUrlLoading" in WebView client.
假如安卓端不方便更新应用了,有没有办法解决呢?答案是肯定的, 我们点进去上面的网址(需要科学上网),发现官方给出的办法:
1、The best practice is to construct an intent anchor and embed that into the page so the user can launch the app. This gives you a lot more flexibility in controlling how apps are launched, including the ability to pass extra information into the app via Intent Extras.
2、Also, you may choose to specify fallback URL by adding the following string extra:
S.browser_fallback_url=[encoded_full_url]
When an intent could not be resolved, or an external application could not be launched, then the user will be redirected to the fallback URL if it was given.
Adding a WebView to your app
1. xml文件中添加webview
2. 加载webview展示网页
3. 添加网络访问权限
Multi-threading and Thread Blocking
如果不是在主线程调用webview,可能会发生不可预料的情况。比如,如果app使用多线程,可以使用runOnUiThread()方法来确保代码在主线程执行
同时也要保证不要block ui线程。在实际使用中常常会出现一些因为调用javascript而发生的问题。比如,不要像以下这样操作。可以使用evaluateJavascript() -----》这个方法在android版本4.4以上支持,主要用于java和js进行交互使用,后续详细说明