Denied starting an intent without a user gesture Webview Android

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

Trying to redirect local html page in android webview using Javascript redirect, gets denied starting an intent in Logcat:

Testing on android 5.1.1

document.location = "index.html";

Denied starting an intent without a user gesture, URI:

file:///android_asset/index.html

回答1:

I read the documentation in 1,000 attempts Android.developer and this was my solution

I do not know if you understand, I speak Spanish

webView.setWebViewClient(new WebViewClient() {     @Override     public boolean shouldOverrideUrlLoading(WebView view, String url) {         return false;                     } }); 


回答2:

This worked for me:

webView.setWebViewClient(new WebViewClient()); 


回答3:

There are few issues here.

  1. From newest androids, the WebView and Chrome Client is separated application which can be automatically updated without user intention.

  2. 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/intents Looks 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.



回答4:

As alternate, i figured out was to add addJavascriptInterface each button click event fire action to JavascriptInterface

webView.addJavascriptInterface(new java2JSAgent(), "java2JSAgentVar"); //webView webview object   public class java2JSAgent {     @JavascriptInterface     public String getContacts()     {         String jsonResponse = "{result:'redirected'}";         runOnUiThread(new Runnable() {              @Override             public void run() {                 webView.loadUrl("file:///android_asset/index.html");             }         });         return jsonResponse;     } } 

might not be a good approach but atleast its working :-) Thanks



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!