Question

I need to know how to wait for event to be triggered to set a Boolean. I got 2 classes simple classes: MainClass and Wait class:

    MainClass()
    {

        WebView wv; //It's already initialized from the xml and linked in the original code
        Wait waiting = new Wait();
        if(waiting(wv,URL))
        {
            wv.loadURL("javascript: ....");
        }

    }

    public Class Wait()
    {
        Boolean pageLoaded = false;
        Wait waiting = this;
        public boolean waitForPage(wv,URL)
        {

            wv.setWebViewClient(new WebViewClient()
            {

                public void onPageFinished(WebView webView, String url )
                {
                    pageLoaded = true;
                    synchronized(waiting)
                    this.nothifyAll();

                }

            });

            wv.loadURL(URL);

            synchronized(this)  // --> End up in an Endless Loop
                this.wait();

            if(pageLoaded)
                return true;

            return false;
        }
    }

Does anybody know why it doesn't get synchronized and finally end up in an endless Loop? Maybe I use the wrong eventhandler? The this.wait(); seems to be like an While(true) verifying on a Boolean set by NoifyAll(). Please some Idea, how I can wait for these Events and Hold the program flow until the event is fired!

Was it helpful?

Solution

The this in synchronized(this) inside the onPageFinished() method refers to the WebViewClient instance, while the this in synchronized(this) after wv.loadURL(URL) refers to the Wait instance.

Consider using the utilities in Android's java.util.concurrent package

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top