Denied starting an intent without a user gesture W

2019-02-16 16:54发布

问题:

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