Denied starting an intent without a user gesture W

2019-02-16 16:49发布

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

4条回答
看我几分像从前
2楼-- · 2019-02-16 17:10

This worked for me:

webView.setWebViewClient(new WebViewClient());
查看更多
爷、活的狠高调
3楼-- · 2019-02-16 17:13

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

查看更多
Viruses.
4楼-- · 2019-02-16 17:29

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;                
    }
});
查看更多
Luminary・发光体
5楼-- · 2019-02-16 17:29

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.

查看更多
登录 后发表回答