WebViewClient not calling shouldOverrideUrlLoading

后端 未结 10 2348
逝去的感伤
逝去的感伤 2020-12-15 18:44

The problem is rather simple. In the application we want to keep track of the current url being displayed. For that we use shouldOverrideUrlLoading callback fr

相关标签:
10条回答
  • 2020-12-15 19:07

    Add webView.getSetting().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); then shouldOverrideUrl will be triggered.

    0 讨论(0)
  • 2020-12-15 19:16

    I had the same problem like you, and I've finished with extending of WebViewChromeClient with listening for callback to

    public void onReceivedTitle(WebView view, String title)
    
    mWebView.setWebChromeClient(mSWWebChromeClient);
    
    private WebChromeClient mSWWebChromeClient = new WebChromeClient() {
    
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (!view.getUrl().equals(mCurrentUrl)) {
                    mCurrentUrl = view.getUrl();
                    //make something
                }
            }
    
        }; 
    
    0 讨论(0)
  • 2020-12-15 19:18
    public class AndroidMobileAppSampleActivity extends Activity {
     /** Called when the activity is first created. */
    String mCurrentUrl="";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        WebView mWebView = (WebView) findViewById(R.id.mainWebView);
    
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    
        mWebView.setWebViewClient(new MyCustomWebViewClient());
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    
        mWebView.loadUrl("https://m.pandora.net/es-es/products/bracelets/556000");
    
    }
    
    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            mCurrentUrl = url;
            Log.i("mCurrentUrl",""+mCurrentUrl);  
            view.loadUrl(url);
    
            return true;
        }
    }
    }
    

    try this one...

    0 讨论(0)
  • 2020-12-15 19:20

    Please omit mWebView.getSettings().setDomStorageEnabled(true);

    Then again try, if a new url found then will invoke shouldOverrideUrl()

    0 讨论(0)
  • 2020-12-15 19:23

    Another approach you can try: Catch the url by javascript side. Initialize your webView with this:

    webView.addJavascriptInterface(new WebAppInterface(getActivity()), "Android");
    

    After page is completely loaded (You can use an algorithm to check this like this https://stackoverflow.com/a/6199854/4198633), then:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        webView.evaluateJavascript("(function() {return window.location.href;})", new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String url) {
                //do your scheme with variable "url"
            }
        });
    } else {
        webView.loadUrl("javascript:Android.getURL(window.location.href);");
    }
    

    And declare your WebAppInterface:

    public class WebAppInterface {
        Activity mContext;
    
        public WebAppInterface(Activity c) {
            mContext = c;
        }
    
        @JavascriptInterface
        public void getURL(final String url) {
            mContext.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //do your scheme with variable "url" in UIThread side. Over here you can call any method inside your activity/fragment
                }
            });
    
        }
    
    }
    

    You can do something like that to get url, or anything else inside the page.

    0 讨论(0)
  • 2020-12-15 19:25

    Maybe this helps someone, although the signature in the question is correct, but Android Studio suggests the following method signature:

    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    

    which then never called. It took me a while to notice that the right signature is:

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    

    Sorry if this not 100% fit the question, but I believe this may help someone in the same situation. It's not always easy to notice that the second parameter is different.

    0 讨论(0)
提交回复
热议问题