1

This is my first app that uses the WebView for my web page.

I want to know how to get the current url from a webview, I'm using this:

        SessionDetector session = new SessionDetector(this);

    webView = (WebView) findViewById(R.id.content);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.loadUrl(AppWebViewUrls.getBaseUrl());

    session.start();

And this class for get the current url:

class SessionDetector extends Thread {
private boolean isLogged = false;
private MainActivity context;

SessionDetector(MainActivity context) {
    this.context = context;
}

@Override
public void run() {
    while (!isLogged) {
        if (context.webView.getUrl().contains("cpanel")) {
            isLogged = true;
        }
    }

    context.createMenu();
}

}

But, Android throws an exception with the message "All webview methods must be called on the same thread".

I would be glad to know how and why is the cause for this error.

6
  • why is the cause? -> All webview methods must be called on the same thread
    – njzk2
    Feb 27, 2014 at 21:46
  • i.e. you can't call webView.getUrl() from SessionDetector extends Thread
    – njzk2
    Feb 27, 2014 at 21:47
  • So is not possible because is an Android security restriction? Feb 27, 2014 at 21:58
  • that's not really the matter. Why do you need to pull constantly the url of the webview? isn't there an event you can catch?
    – njzk2
    Feb 27, 2014 at 22:37
  • Thanks for your help njzk2 I found the solution, I was very wrong. Feb 27, 2014 at 22:57

1 Answer 1

-1

I've found that shouldOverrideUrlLoading method works fine for my case.

            @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            super.shouldOverrideUrlLoading(view, url);

            if(url.contains("cpanel")) {
                createMenu();
            }               

            return true;
        }
5
  • Please remove the call to loadUrl from shouldOverrideUrlLoading. It is not needed and creates a loop.
    – ksasq
    Feb 28, 2014 at 8:47
  • @ksasq : since the return value is true, no, it does not create a loop. (but it still stands that this is redundant from the default behavior)
    – njzk2
    Feb 28, 2014 at 13:58
  • @njzk2 it will cause another (asynchronous) call to shouldOverrideUrlLoading, which will then call loadUrl, etc.
    – ksasq
    Feb 28, 2014 at 14:04
  • I don't think calling loadUrl triggers a call to shouldOverrideUrlLoading.
    – njzk2
    Feb 28, 2014 at 15:17
  • @njzk2 - calling loadUrl from shouldOverrideUrlLoading is a bad idea, see what happens if you load a page like this into a webview that does that: jsbin.com/gupug/1/quiet Mar 4, 2014 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.